DEADSOFTWARE

2974fdb73063474173b1ed42272d2b8a157efee9
[d2df-sdl.git] / src / nogl / noGLALSW.inc
1 implementation
3 uses Allegro, Math, SDL2, e_Log;
5 const
6 GL_INVALID_ENUM = $0500;
8 const
9 ValPerVertex = 2;
10 ValPerColor = 1; (* colors stored in one integer *)
11 ValPerCoord = 2;
12 StackSize = 16;
14 type
15 TArrayFloat = array of GLfloat;
16 TArrayInteger = array of Integer;
18 TCmds = record
19 mode: GLenum;
20 v: TArrayInteger;
21 c: TArrayInteger;
22 t: TArrayFloat;
23 end;
25 TArrayTexture = array of record
26 used: Boolean;
27 bmp: PBITMAP;
28 end;
30 var
31 cmds: TCmds;
32 tex: TArrayTexture;
33 ctex: Integer;
34 ccol: Integer;
35 clearColor: cint;
36 stack: array [0..StackSize - 1] of record
37 x, y, a: Integer;
38 end;
39 stack_ptr: Integer;
40 vpx, vpy: Integer;
41 pointSize: Integer;
42 matrixMode: GLenum;
44 function AddTexture: Integer;
45 var i: Integer;
46 begin
47 for i := 1 to High(tex) do
48 if not tex[i].used then
49 begin
50 tex[i].used := true;
51 tex[i].bmp := nil;
52 result := i;
53 exit
54 end;
55 i := Length(tex);
56 SetLength(tex, i + 1);
57 tex[i].used := true;
58 tex[i].bmp := nil;
59 result := i
60 end;
62 procedure RemoveTexture(i: Integer);
63 begin
64 assert(i >= 0);
65 assert(i <= High(tex));
66 assert((i = 0) or tex[i].used); (* free unallocated texture *)
67 tex[i].used := false;
68 if tex[i].bmp <> nil then
69 destroy_bitmap(tex[i].bmp);
70 tex[i].bmp := nil
71 end;
73 procedure Addi (var x: TArrayInteger; f: Integer);
74 var i: Integer;
75 begin
76 i := Length(x);
77 SetLength(x, i + 1);
78 x[i] := f;
79 end;
81 procedure Addf (var x: TArrayFloat; f: GLfloat);
82 var i: Integer;
83 begin
84 i := Length(x);
85 SetLength(x, i + 1);
86 x[i] := f;
87 end;
89 (** Open GL **)
91 procedure glEnable(cap: GLenum);
92 begin
93 end;
95 procedure glDisable(cap: GLenum);
96 begin
97 end;
99 function glIsEnabled(cap: GLenum): GLboolean;
100 begin
101 result := 0
102 end;
104 function glGetString(name: GLenum): PChar;
105 begin
106 if name = GL_EXTENSIONS then (* separated by space *)
107 result := 'GL_ARB_texture_non_power_of_two'
108 else
109 result := nil
110 end;
112 procedure glClearColor(red, green, blue, alpha: GLclampf);
113 begin
114 clearColor := makeacol(floor(red * 255), floor(green * 255), floor(blue * 255), floor(alpha * 255));
115 end;
117 procedure glClear(mask: GLbitfield);
118 begin
119 if (mask and GL_COLOR_BUFFER_BIT) <> 0 then
120 clear_to_color(sdl2allegro_screen, clearColor)
121 end;
123 procedure glAlphaFunc(func: GLenum; ref: GLclampf);
124 begin
125 end;
127 procedure glBlendFunc(sfactor, dfactor: GLenum);
128 begin
129 end;
131 procedure glPointSize(size: GLfloat);
132 begin
133 ASSERT(size >= 0);
134 if size <= 1.0 then pointSize := ceil(size)
135 else pointSize := floor(size)
136 end;
138 procedure glLineWidth(width: GLfloat);
139 begin
140 (* width > 1 used in rare cases, not critical *)
141 end;
143 procedure glGetIntegerv(pname: GLenum; params: PGLint);
144 begin
145 params^ := 0
146 end;
148 procedure glFlush;
149 begin
150 end;
152 procedure glFinish;
153 begin
154 end;
156 procedure glBegin(mode: GLenum);
157 begin
158 assert(cmds.mode = GL_INVALID_ENUM);
159 assert((mode = GL_POINTS) or (mode = GL_LINES) or (mode = GL_QUADS));
160 cmds.mode := mode;
161 SetLength(cmds.v, 0);
162 SetLength(cmds.c, 0);
163 SetLength(cmds.t, 0);
164 end;
166 procedure glEnd;
167 var
168 i, j, k, w, h, x0, y0, x1, y1, offx, offy, tmp, s0, t0, s1, t1, angle: Integer;
169 oldx0, oldy0, oldx1, oldy1: cint;
170 flipv, fliph: Boolean;
171 draw_sprite_proc: procedure (bmp, sprite: Allegro.PBITMAP; x, y: cint); cdecl;
172 rotate_sprite_proc: procedure (bmp, sprite: Allegro.PBITMAP; x, y: cint; a: cint32); cdecl;
173 begin
174 assert(cmds.mode <> GL_INVALID_ENUM);
175 assert(Length(cmds.v) mod ValPerVertex = 0);
176 assert(Length(cmds.c) mod ValPerColor = 0);
177 assert(Length(cmds.t) mod ValPerCoord = 0);
179 offx := vpx + stack[stack_ptr].x;
180 offy := vpy + stack[stack_ptr].y;
181 angle := stack[stack_ptr].a;
183 case cmds.mode of
184 GL_POINTS:
185 begin
186 (* implement case for texture coords? *)
187 if pointSize = 1 then
188 begin
189 if Length(cmds.c) <> 0 then
190 begin
191 assert(Length(cmds.c) * 2 = Length(cmds.v)); (* not enough colors *)
192 for i := 0 to Length(cmds.v) div 2 - 1 do
193 putpixel(sdl2allegro_screen, offx + cmds.v[i * 2], offy + cmds.v[i * 2 + 1], cmds.c[i])
194 end
195 else
196 begin
197 for i := 0 to Length(cmds.v) div 2 - 1 do
198 putpixel(sdl2allegro_screen, offx + cmds.v[i * 2], offy + cmds.v[i * 2 + 1], ccol)
199 end
200 end
201 else if pointSize > 1 then
202 begin
203 x0 := offx - pointSize div 2;
204 y0 := offy - pointSize div 2;
205 x1 := offx - (pointSize - 1) div 2;
206 y1 := offy - (pointSize - 1) div 2;
207 if Length(cmds.c) <> 0 then
208 begin
209 assert(Length(cmds.c) * 2 = Length(cmds.v)); (* not enough colors *)
210 for i := 0 to Length(cmds.v) div 2 - 1 do
211 begin
212 w := cmds.v[i * 2 + 0];
213 h := cmds.v[i * 2 + 1];
214 rectfill(sdl2allegro_screen, x0 + w, y0 + h, x1 + w, y1 + h, cmds.c[i])
215 end
216 end
217 else
218 begin
219 for i := 0 to Length(cmds.v) div 2 - 1 do
220 begin
221 w := cmds.v[i * 2 + 0];
222 h := cmds.v[i * 2 + 1];
223 rectfill(sdl2allegro_screen, x0 + w, y0 + h, x1 + w, y1 + h, ccol)
224 end
225 end
226 end
227 end;
228 GL_LINES:
229 begin
230 assert(Length(cmds.v) mod 4 = 0); (* broken line *)
231 (* implement case for texture coords? *)
232 if Length(cmds.c) <> 0 then
233 begin
234 assert(Length(cmds.c) * 2 = Length(cmds.v));
235 for i := 0 to Length(cmds.v) div 4 - 1 do
236 fastline(sdl2allegro_screen, offx + cmds.v[i * 4], offy + cmds.v[i * 4 + 1], offx + cmds.v[i * 4 + 2], offy + cmds.v[i * 4 + 3], cmds.c[i * 2])
237 end
238 else
239 begin
240 for i := 0 to Length(cmds.v) div 4 - 1 do
241 fastline(sdl2allegro_screen, offx + cmds.v[i * 4], offy + cmds.v[i * 4 + 1], offx + cmds.v[i * 4 + 2], offy + cmds.v[i * 4 + 3], ccol)
242 end
243 end;
244 GL_QUADS:
245 begin
246 ASSERT(Length(cmds.v) mod 8 = 0); (* broken quad *)
247 if Length(cmds.t) <> 0 then
248 begin
249 ASSERT(Length(cmds.t) = Length(cmds.v)); (* not enough texture coords *)
250 ASSERT(ctex >= 0);
251 ASSERT(ctex <= High(tex));
252 ASSERT(tex[ctex].bmp <> nil);
253 for i := 0 to Length(cmds.v) div 8 - 1 do
254 begin
255 flipv := False; fliph := False;
256 x0 := cmds.v[i * 8 + 0]; y0 := cmds.v[i * 8 + 1];
257 x1 := cmds.v[i * 8 + 4]; y1 := cmds.v[i * 8 + 5];
258 if x1 < x0 then
259 begin
260 tmp := x0;
261 x0 := x1;
262 x1 := tmp;
263 fliph := not fliph
264 end;
265 if y1 < y0 then
266 begin
267 tmp := y0;
268 y0 := y1;
269 y1 := tmp;
270 flipv := not flipv
271 end;
273 w := tex[ctex].bmp.w;
274 h := tex[ctex].bmp.h;
275 s0 := Trunc(cmds.t[i * 8 + 0] * w);
276 t0 := Trunc(cmds.t[i * 8 + 1] * h);
277 s1 := Trunc(cmds.t[i * 8 + 4] * w);
278 t1 := Trunc(cmds.t[i * 8 + 5] * h);
280 if s1 < s0 then
281 begin
282 tmp := s0;
283 s0 := s1;
284 s1 := tmp;
285 fliph := not fliph;
286 end;
287 if t1 < t0 then
288 begin
289 tmp := t0;
290 t0 := t1;
291 t1 := tmp;
292 flipv := not flipv;
293 end;
295 if fliph then
296 begin
297 tmp := s0;
298 s0 := w - s1;
299 s1 := w - tmp;
300 end;
301 if flipv then
302 begin
303 tmp := t0;
304 t0 := h - t1;
305 t1 := h - tmp;
306 end;
308 s0 := s0 mod w;
309 t0 := t0 mod h;
310 s1 := s1 mod w;
311 t1 := t1 mod h;
313 if flipv and fliph then
314 draw_sprite_proc := Allegro.draw_sprite_vh_flip
315 else if flipv then
316 draw_sprite_proc := Allegro.draw_sprite_v_flip
317 else if fliph then
318 draw_sprite_proc := Allegro.draw_sprite_h_flip
319 else
320 draw_sprite_proc := Allegro.draw_sprite;
322 if flipv and fliph then
323 rotate_sprite_proc := Allegro.rotate_sprite_v_flip (* ??? *)
324 else if flipv then
325 rotate_sprite_proc := Allegro.rotate_sprite_v_flip
326 else if fliph then
327 rotate_sprite_proc := Allegro.rotate_sprite (* ??? *)
328 else
329 rotate_sprite_proc := Allegro.rotate_sprite;
331 oldx0 := 0; oldy0 := 0; oldx1 := 0; oldy1 := 0;
332 get_clip_rect(sdl2allegro_screen, oldx0, oldy0, oldx1, oldy1);
333 set_clip_rect(sdl2allegro_screen, max(oldx0, offx + x0), max(oldy0, offy + y0), min(oldx1, offx + x1), min(oldy1, offy + y1));
335 if angle = 0 then
336 for j := 0 to (y1 - y0 + h - 1) div h - 1 do
337 for k := 0 to (x1 - x0 + w - 1) div w - 1 do
338 draw_sprite_proc(sdl2allegro_screen, tex[ctex].bmp, offx + x0 + k * w - s0, offy + y0 + j * h - t0)
339 else
340 for j := 0 to (y1 - y0 + h - 1) div h - 1 do
341 for k := 0 to (x1 - x0 + w - 1) div w - 1 do
342 rotate_sprite_proc(sdl2allegro_screen, tex[ctex].bmp, offx + x0 + k * w - s0, offy + y0 + j * h - t0, angle);
344 set_clip_rect(sdl2allegro_screen, oldx0, oldy0, oldx1, oldy1);
346 //rect(sdl2allegro_screen, offx + x0, offy + y0, offx + x1, offy + y1, makecol(255, 0, 0));
347 //rect(sdl2allegro_screen, offx + oldx0, offy + oldy0, offx + oldx1, offy + oldx1, makecol(0, 255, 0));
348 end
349 end
350 else if Length(cmds.c) <> 0 then
351 begin
352 assert(Length(cmds.c) * 2 = Length(cmds.v)); (* not enough colors *)
353 for i := 0 to Length(cmds.v) div 8 - 1 do
354 rectfill(sdl2allegro_screen, offx + cmds.v[i * 8], offy + cmds.v[i * 8 + 1], offx + cmds.v[i * 8 + 4], offy + cmds.v[i * 8 + 5], cmds.c[i * 4])
355 end
356 else
357 begin
358 for i := 0 to Length(cmds.v) div 8 - 1 do
359 rectfill(sdl2allegro_screen, offx + cmds.v[i * 8], offy + cmds.v[i * 8 + 1], offx + cmds.v[i * 8 + 4], offy + cmds.v[i * 8 + 5], ccol)
360 end
361 end;
362 else
363 assert(false)
364 end;
366 SetLength(cmds.v, 0);
367 SetLength(cmds.c, 0);
368 SetLength(cmds.t, 0);
369 cmds.mode := GL_INVALID_ENUM;
370 end;
372 procedure glVertex2f(x, y: GLfloat);
373 begin
374 Addi(cmds.v, ceil(x));
375 Addi(cmds.v, ceil(y))
376 end;
378 procedure glVertex2i(x, y: GLint);
379 begin
380 Addi(cmds.v, x);
381 Addi(cmds.v, y)
382 end;
384 procedure glColor4f(red, green, blue, alpha: GLfloat);
385 begin
386 ccol := makeacol(floor(red * 255), floor(green * 255), floor(blue * 255), floor(alpha * 255));
387 Addi(cmds.c, ccol)
388 end;
390 procedure glColor4ub(red, green, blue, alpha: GLubyte);
391 begin
392 ccol := makeacol(red, green, blue, alpha);
393 Addi(cmds.c, ccol)
394 end;
396 procedure glColor3ub(red, green, blue: GLubyte);
397 begin
398 ccol := makecol(red, green, blue);
399 Addi(cmds.c, ccol)
400 end;
402 procedure glTexCoord2f(s, t: GLfloat);
403 begin
404 Addf(cmds.t, s);
405 Addf(cmds.t, t);
406 end;
408 procedure glTexCoord2i(s, t: GLint);
409 begin
410 Addf(cmds.t, s);
411 Addf(cmds.t, t);
412 end;
414 procedure glReadPixels(x, y: GLint; width, height: GLsizei; format, atype: GLenum; pixels: Pointer);
415 begin
416 end;
418 procedure glLoadIdentity;
419 begin
420 if matrixMode <> GL_MODELVIEW then Exit;
421 with stack[stack_ptr] do
422 begin
423 x := 0;
424 y := 0;
425 (* TODO Rotation and scale *)
426 end
427 end;
429 procedure glMatrixMode(mode: GLenum);
430 begin
431 (* GL_PROJECTION -> verify or ignore *)
432 (* GL_MODELVIEW -> apply *)
433 ASSERT((mode = GL_PROJECTION) or (mode = GL_MODELVIEW));
434 matrixMode := mode;
435 end;
437 procedure glLoadMatrixd(const m: PGLdouble);
438 begin
439 if matrixMode <> GL_MODELVIEW then Exit;
441 (*
442 e_LogWritefln('glLoadMatrix:', []);
443 e_LogWritefln('| %s %s %s %s |', [m[0], m[1], m[2], m[3]]);
444 e_LogWritefln('| %s %s %s %s |', [m[4], m[5], m[6], m[7]]);
445 e_LogWritefln('| %s %s %s %s |', [m[8], m[9], m[10], m[11]]);
446 e_LogWritefln('| %s %s %s %s |', [m[12], m[13], m[14], m[15]]);
447 *)
448 with stack[stack_ptr] do
449 begin
450 x := Trunc(m[3]);
451 y := Trunc(m[7]);
452 ASSERT(m[11] = 0);
453 (* TODO Rotation and Scale *)
454 end
455 end;
457 procedure glPushMatrix;
458 begin
459 if matrixMode <> GL_MODELVIEW then Exit;
460 stack[stack_ptr + 1] := stack[stack_ptr];
461 INC(stack_ptr);
462 end;
464 procedure glPopMatrix;
465 begin
466 if matrixMode <> GL_MODELVIEW then Exit;
467 DEC(stack_ptr)
468 end;
470 procedure glTranslatef(x, y, z: GLfloat);
471 begin
472 if matrixMode <> GL_MODELVIEW then Exit;
473 ASSERT(z = 0); (* 3D not supported *)
474 stack[stack_ptr].x += Trunc(x);
475 stack[stack_ptr].y += Trunc(y);
476 end;
478 procedure glRotatef(angle, x, y, z: GLfloat);
479 begin
480 if matrixMode <> GL_MODELVIEW then Exit;
481 ASSERT(x = 0); (* 3D not supported *)
482 ASSERT(y = 0); (* 3D not supported *)
483 // angle 360deg == 256 with conversion to fixed point 16.16
484 stack[stack_ptr].a += floor(angle * z * 0.71111) * 65536
485 end;
487 procedure glScalef(x, y, z: GLfloat);
488 begin
489 if matrixMode <> GL_MODELVIEW then Exit;
490 (* 3D not supported, but z can be any *)
491 (* TODO Scale *)
492 end;
494 procedure glViewport(x, y: GLint; width, height: GLsizei);
495 begin
496 vpx := x; vpy := y;
497 set_clip_rect(sdl2allegro_screen, x, y, x + width, y + height);
498 end;
500 procedure glScissor(x, y: GLint; width, height: GLsizei);
501 begin
502 //set_clip_rect(sdl2allegro_screen, x, y, width, height)
503 end;
505 procedure glStencilMask(mask: GLuint);
506 begin
507 end;
509 procedure glStencilFunc(func: GLenum; ref: GLint; mask: GLuint);
510 begin
511 end;
513 procedure glStencilOp(fail, zfail, zpass: GLenum);
514 begin
515 end;
517 procedure glColorMask(red, green, blue, alpha: GLboolean);
518 begin
519 end;
521 procedure glBindTexture(target: GLenum; texture: GLuint);
522 begin
523 assert(target = GL_TEXTURE_2D);
524 ctex := texture;
525 end;
527 procedure glGenTextures(n: GLsizei; textures: PGLuint);
528 var i: Integer;
529 begin
530 for i := 0 to n - 1 do
531 textures[i] := AddTexture
532 end;
534 procedure glTexEnvi(target: GLenum; pname: GLenum; param: GLint);
535 begin
536 end;
538 procedure glTexParameterf(target: GLenum; pname: GLenum; param: GLfloat);
539 begin
540 end;
542 procedure glTexParameteri(target: GLenum; pname: GLenum; param: GLint);
543 begin
544 end;
546 procedure glTexImage2D(target: GLenum; level, internalformat: GLint; width, height: GLsizei; border: GLint; format, atype: GLenum; const pixels: Pointer);
547 var i, j, adr: Integer; p: PByte; color, trans: cint;
548 begin
549 assert(target = GL_TEXTURE_2D);
550 assert(level = 0);
551 assert((internalformat = GL_RGBA) or (internalformat = GL_RGB));
552 assert((format = GL_RGBA) or (format = GL_RGB));
553 assert(border = 0);
554 assert(atype = GL_UNSIGNED_BYTE);
556 assert(ctex >= 0);
557 assert(ctex <= High(tex));
558 assert(tex[ctex].used);
560 if tex[ctex].bmp <> nil then
561 destroy_bitmap(tex[ctex].bmp);
562 tex[ctex].bmp := create_system_bitmap(width, height);
563 if tex[ctex].bmp = nil then
564 tex[ctex].bmp := create_bitmap(width, height);
565 assert(tex[ctex].bmp <> nil);
567 if pixels = nil then exit;
569 p := pixels;
570 if format = GL_RGBA then
571 begin
572 if DEFAULT_DEPTH <= 8 then
573 trans := 0
574 else
575 trans := makeacol(255, 0, 255, 0);
577 for j := 0 to height - 1 do
578 for i := 0 to width - 1 do
579 begin
580 adr := j * width * 4 + i * 4;
581 if p[adr + 3] = 0 then
582 color := trans
583 else
584 color := makeacol(p[adr], p[adr + 1], p[adr + 2], p[adr + 3]);
585 putpixel(tex[ctex].bmp, i, j, color)
586 end
587 end
588 else
589 begin
590 for j := 0 to height - 1 do
591 for i := 0 to width - 1 do
592 begin
593 adr := j * width * 3 + i * 3;
594 putpixel(tex[ctex].bmp, i, j, makecol(p[adr], p[adr + 1], p[adr + 2]))
595 end
596 end
597 end;
599 procedure glTexSubImage2D(target: GLenum; level, xoffset, yoffset: GLint; width, height: GLsizei; format, atype: GLenum; const pixels: Pointer);
600 var i, j, adr: Integer; p: PByte; color, trans: Cint;
601 begin
602 assert(target = GL_TEXTURE_2D);
603 assert(level = 0);
604 assert((format = GL_RGBA) or (format = GL_RGB));
605 assert(atype = GL_UNSIGNED_BYTE);
607 assert(ctex >= 0);
608 assert(ctex <= High(tex));
609 assert(tex[ctex].used);
611 assert(xoffset = 0);
612 assert(yoffset = 0);
614 if pixels = nil then exit;
616 p := pixels;
617 if format = GL_RGBA then
618 begin
619 if DEFAULT_DEPTH <= 8 then
620 trans := 0
621 else
622 trans := makeacol(255, 0, 255, 0);
624 for j := 0 to height - 1 do
625 for i := 0 to width - 1 do
626 begin
627 adr := j * width * 4 + i * 4;
628 if p[adr + 3] = 0 then
629 color := trans
630 else
631 color := makeacol(p[adr], p[adr + 1], p[adr + 2], p[adr + 3]);
632 putpixel(tex[ctex].bmp, i, j, color)
633 end
634 end
635 else
636 begin
637 for j := 0 to height - 1 do
638 for i := 0 to width - 1 do
639 begin
640 adr := j * width * 3 + i * 3;
641 putpixel(tex[ctex].bmp, i, j, makecol(p[adr], p[adr + 1], p[adr + 2]))
642 end
643 end
644 end;
646 procedure glDeleteTextures(n: GLsizei; const textures: PGLuint);
647 var i: Integer;
648 begin
649 for i := 0 to n - 1 do
650 RemoveTexture(textures[i])
651 end;
653 procedure nogl_Init;
654 begin
655 cmds.mode := GL_INVALID_ENUM
656 end;
658 procedure nogl_Quit;
659 begin
660 end;
662 initialization