texture mapping and shading LUTs
[metatoy] / src / kern / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <limits.h>
5 #include "segm.h"
6 #include "intr.h"
7 #include "mem.h"
8 #include "contty.h"
9 #include "keyb.h"
10 #include "psaux.h"
11 #include "timer.h"
12 #include "game.h"
13
14 unsigned char *framebuf, *vmem = (unsigned char*)0xa0000;
15
16 static int quit;
17 static int mx, my;
18 static long last_mouse_ev;
19
20 #define CURSOR_TIMEOUT  MSEC_TO_TICKS(3000)
21 static void draw_cursor(int mx, int my);
22
23
24 int main(void)
25 {
26         int key;
27         int prev_mx = 0, prev_my = 0;
28         unsigned int mbn, prev_mbn = 0, mbn_diff;
29
30         init_segm();
31         init_intr();
32         con_init();
33         kb_init();
34         init_psaux();
35         init_mem();
36         init_timer();
37         enable_intr();
38
39         if(!(framebuf = malloc(320 * 200))) {
40                 printf("failed to allocate framebuffer\n");
41                 goto end;
42         }
43
44         if(game_init() == -1) {
45                 goto end;
46         }
47
48         set_mouse_pos(160, 100);
49         last_mouse_ev = LONG_MIN;
50
51         for(;;) {
52                 if((key = kb_getkey()) >= 0) {
53                         game_keyboard(key, 1);
54                 }
55
56                 mbn = mouse_state(&mx, &my);
57
58                 if((mbn_diff = mbn ^ prev_mbn) != 0) {
59                         if(mbn_diff & 1) {
60                                 game_mouse(0, mbn & 1, mx, my);
61                                 last_mouse_ev = nticks;
62                         }
63                         if(mbn_diff & 2) {
64                                 game_mouse(2, mbn & 2, mx, my);
65                                 last_mouse_ev = nticks;
66                         }
67                         if(mbn_diff & 4) {
68                                 game_mouse(1, mbn & 4, mx, my);
69                                 last_mouse_ev = nticks;
70                         }
71                 }
72                 if(mx != prev_mx || my != prev_my) {
73                         game_motion(mx, my);
74                         last_mouse_ev = nticks;
75                 }
76                 prev_mbn = mbn;
77                 prev_mx = mx;
78                 prev_my = my;
79
80                 if(quit) break;
81                 game_draw();
82         }
83         game_shutdown();
84
85 end:
86         cleanup_intr(); /* also disables interrupts */
87         cleanup_timer();
88         return 0;
89 }
90
91 unsigned long game_getmsec(void)
92 {
93         return TICKS_TO_MSEC(nticks);
94 }
95
96 void game_quit(void)
97 {
98         quit = 1;
99 }
100
101 void game_swap_buffers(void)
102 {
103         if((long)nticks - last_mouse_ev < MSEC_TO_TICKS(3000)) {
104                 draw_cursor(mx, my);
105         }
106
107         memcpy(vmem, framebuf, 64000);
108 }
109
110 #define CUR_SZ  4
111
112 static void draw_cursor(int mx, int my)
113 {
114         int i, sx, ex, sy, ey, xlen, ylen;
115         unsigned char *ptr;
116
117         sx = mx < CUR_SZ ? mx : mx - CUR_SZ;
118         ex = mx >= 320 - CUR_SZ ? CUR_SZ - 1 : mx + CUR_SZ;
119         xlen = ex - sx + 1;
120         sy = my < CUR_SZ ? my : my - CUR_SZ;
121         ey = my >= 200 - CUR_SZ ? CUR_SZ - 1 : my + CUR_SZ;
122         ylen = ey - sy + 1;
123
124         ptr = framebuf + (my << 8) + (my << 6) + sx;
125         for(i=0; i<xlen; i++) {
126                 ptr[i] ^= 0xff;
127         }
128
129         ptr = framebuf + (sy << 8) + (sy << 6) + mx;
130         for(i=0; i<ylen; i++) {
131                 *ptr ^= 0xff;
132                 ptr += 320;
133         }
134 }