Skip to content

Commit 26719ab

Browse files
committed
replace drawing primitives with faster ones from SDL_gfx
1 parent 3db6678 commit 26719ab

File tree

2 files changed

+9
-128
lines changed

2 files changed

+9
-128
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
all: load81
22

33
load81: load81.c lua/src/liblua.a bitfont.h
4-
$(CC) -Ilua/src -O2 -Wall -W $< `sdl-config --cflags` `sdl-config --libs` -lm lua/src/liblua.a -o $@
4+
$(CC) -Ilua/src -O2 -Wall -W $< `sdl-config --cflags` `sdl-config --libs` -lm -lSDL_gfx lua/src/liblua.a -o $@
55

66
lua/src/liblua.a:
77
-(cd lua && $(MAKE) ansi)

load81.c

+8-127
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <ctype.h>
3232

3333
#include <SDL.h>
34+
#include <SDL_gfxPrimitives.h>
3435
#include <lua.h>
3536
#include <lauxlib.h>
3637
#include <lualib.h>
@@ -165,151 +166,31 @@ frameBuffer *createFrameBuffer(int width, int height) {
165166
/* ========================== Drawing primitives ============================ */
166167

167168
void setPixelWithAlpha(frameBuffer *fb, int x, int y, int r, int g, int b, float alpha) {
168-
unsigned char *p;
169-
170-
if (x < 0 || x >= fb->width || y < 0 || y >= fb->height) return;
171-
y = fb->height-1-y; /* y=0 is bottom border of the screen */
172-
p = l81.fb->screen->pixels+y*l81.fb->screen->pitch+(x*3);
173-
174-
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
175-
p[0] = (alpha*b)+((1-alpha)*p[0]);
176-
p[1] = (alpha*g)+((1-alpha)*p[1]);
177-
p[2] = (alpha*r)+((1-alpha)*p[2]);
178-
#else
179-
p[0] = (alpha*r)+((1-alpha)*p[0]);
180-
p[1] = (alpha*g)+((1-alpha)*p[1]);
181-
p[2] = (alpha*b)+((1-alpha)*p[2]);
182-
#endif
169+
pixelRGBA(fb->screen, x, fb->height-1-y, r, g, b, alpha*255);
183170
}
184171

185172
void fillBackground(frameBuffer *fb, int r, int g, int b) {
186-
int x, y;
187-
unsigned char *s;
188-
189-
for (y = 0; y < fb->height; y++) {
190-
s = fb->screen->pixels+y*(fb->screen->pitch);
191-
for (x = 0; x < fb->width; x++) {
192-
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
193-
s[0] = b;
194-
s[1] = g;
195-
s[2] = r;
196-
#else
197-
s[0] = r;
198-
s[1] = g;
199-
s[2] = b;
200-
#endif
201-
s += 3;
202-
}
203-
}
173+
boxRGBA(fb->screen, 0, 0, fb->width-1, fb->height-1, r, g, b, 255);
204174
}
205175

206176
void drawHline(frameBuffer *fb, int x1, int x2, int y, int r, int g, int b, float alpha) {
207-
int aux, x;
208-
209-
if (x1 > x2) {
210-
aux = x1;
211-
x1 = x2;
212-
x2 = aux;
213-
}
214-
for (x = x1; x <= x2; x++)
215-
setPixelWithAlpha(fb,x,y,r,g,b,alpha);
177+
hlineRGBA(fb->screen, x1, x2, fb->height-1-y, r, g, b, alpha*255);
216178
}
217179

218180
void drawEllipse(frameBuffer *fb, int xc, int yc, int radx, int rady, int r, int g, int b, float alpha) {
219-
int x1, x2, y;
220-
float xshift;
221-
222-
for (y=yc-rady; y<=yc+rady; y++) {
223-
xshift = sqrt((rady*rady) - ((y - yc)*(y - yc)))*((float)radx/rady);
224-
x1 = round(xc-xshift);
225-
x2 = round(xc+xshift);
226-
drawHline(fb,x1,x2,y,r,g,b,alpha);
227-
}
181+
filledEllipseRGBA(fb->screen, xc, fb->height-1-yc, radx, rady, r, g, b, alpha*255);
228182
}
229183

230184
void drawBox(frameBuffer *fb, int x1, int y1, int x2, int y2, int r, int g, int b, float alpha) {
231-
int x, y;
232-
233-
for (x = x1; x <= x2; x++ ) {
234-
for (y = y1; y <= y2; y++) {
235-
setPixelWithAlpha(fb,x,y,r,g,b,alpha);
236-
}
237-
}
185+
boxRGBA(fb->screen, x1, fb->height-1-y1, x2, fb->height-1-y2, r, g, b, alpha*255);
238186
}
239187

240188
void drawTriangle(frameBuffer *fb, int x1, int y1, int x2, int y2, int x3, int y3, int r, int g, int b, float alpha) {
241-
int swap, t;
242-
struct {
243-
float x, y;
244-
} A, B, C, E, S;
245-
float dx1,dx2,dx3;
246-
247-
A.x = x1;
248-
A.y = y1;
249-
B.x = x2;
250-
B.y = y2;
251-
C.x = x3;
252-
C.y = y3;
253-
254-
/* For this algorithm to work we need to sort A, B, C by 'y' */
255-
do {
256-
swap = 0;
257-
if (A.y > B.y) {
258-
t = A.y; A.y = B.y; B.y = t;
259-
t = A.x; A.x = B.x; B.x = t;
260-
swap++;
261-
}
262-
if (B.y > C.y) {
263-
t = B.y; B.y = C.y; C.y = t;
264-
t = B.x; B.x = C.x; C.x = t;
265-
swap++;
266-
}
267-
} while(swap);
268-
269-
if (B.y-A.y > 0) dx1=(B.x-A.x)/(B.y-A.y); else dx1=B.x - A.x;
270-
if (C.y-A.y > 0) dx2=(C.x-A.x)/(C.y-A.y); else dx2=0;
271-
if (C.y-B.y > 0) dx3=(C.x-B.x)/(C.y-B.y); else dx3=0;
272-
273-
S=E=A;
274-
if(dx1 > dx2) {
275-
for(;S.y<=B.y;S.y++,E.y++,S.x+=dx2,E.x+=dx1)
276-
drawHline(fb,S.x,E.x,S.y,r,g,b,alpha);
277-
E=B;
278-
E.y+=1;
279-
for(;S.y<=C.y;S.y++,E.y++,S.x+=dx2,E.x+=dx3)
280-
drawHline(fb,S.x,E.x,S.y,r,g,b,alpha);
281-
} else {
282-
for(;S.y<=B.y;S.y++,E.y++,S.x+=dx1,E.x+=dx2)
283-
drawHline(fb,S.x,E.x,S.y,r,g,b,alpha);
284-
S=B;
285-
S.y+=1;
286-
for(;S.y<=C.y;S.y++,E.y++,S.x+=dx3,E.x+=dx2)
287-
drawHline(fb,S.x,E.x,S.y,r,g,b,alpha);
288-
}
189+
filledTrigonRGBA(fb->screen, x1, fb->height-1-y1, x2, fb->height-1-y2, x3, fb->height-1-y3, r, g, b, alpha*255);
289190
}
290191

291-
292-
/* Bresenham algorithm */
293192
void drawLine(frameBuffer *fb, int x1, int y1, int x2, int y2, int r, int g, int b, float alpha) {
294-
int dx = abs(x2-x1);
295-
int dy = abs(y2-y1);
296-
int sx = (x1 < x2) ? 1 : -1;
297-
int sy = (y1 < y2) ? 1 : -1;
298-
int err = dx-dy, e2;
299-
300-
while(1) {
301-
setPixelWithAlpha(fb,x1,y1,r,g,b,alpha);
302-
if (x1 == x2 && y1 == y2) break;
303-
e2 = err*2;
304-
if (e2 > -dy) {
305-
err -= dy;
306-
x1 += sx;
307-
}
308-
if (e2 < dx) {
309-
err += dx;
310-
y1 += sy;
311-
}
312-
}
193+
lineRGBA(fb->screen, x1, fb->height-1-y1, x2, fb->height-1-y2, r, g, b, alpha*255);
313194
}
314195

315196
/* ============================= Bitmap font =============================== */

0 commit comments

Comments
 (0)