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