12 static void handle_event(SDL_Event *ev);
13 static void toggle_fullscreen(void);
15 static int sdlkey_to_gamekey(int sdlkey, unsigned int mod);
19 static SDL_Surface *fbsurf;
21 static int fbscale = 2;
23 static unsigned int sdl_flags = SDL_SWSURFACE;
26 int main(int argc, char **argv)
31 if((env = getenv("FBSCALE")) && (s = atoi(env))) {
33 printf("Framebuffer scaling x%d\n", fbscale);
36 xsz = FB_WIDTH * fbscale;
37 ysz = FB_HEIGHT * fbscale;
39 /* allocate 1 extra row as a guard band, until we fucking fix the rasterizer */
40 if(!(fb_pixels = malloc(FB_WIDTH * (FB_HEIGHT + 1) * FB_BPP / CHAR_BIT))) {
41 fprintf(stderr, "failed to allocate virtual framebuffer\n");
45 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
46 if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, sdl_flags))) {
47 fprintf(stderr, "failed to set video mode %dx%d %dbpp\n", FB_WIDTH, FB_HEIGHT, FB_BPP);
52 SDL_WM_SetCaption("eradicate/SDL", 0);
56 if(init(argc, argv) == -1) {
66 while(SDL_PollEvent(&ev)) {
68 if(quit) goto break_evloop;
71 time_msec = get_msec();
88 unsigned long start = SDL_GetTicks();
89 unsigned long until = (start | 0xf) + 1;
90 while(SDL_GetTicks() <= until);
93 void blit_frame(void *pixels, int vsync)
96 unsigned short *sptr, *dptr;
102 if(SDL_MUSTLOCK(fbsurf)) {
103 SDL_LockSurface(fbsurf);
107 dptr = (unsigned short*)fbsurf->pixels + (fbsurf->w - xsz) / 2;
108 for(i=0; i<FB_HEIGHT; i++) {
109 for(j=0; j<FB_WIDTH; j++) {
111 unsigned short pixel = *sptr++;
113 for(y=0; y<fbscale; y++) {
114 for(x=0; x<fbscale; x++) {
115 dptr[y * fbsurf->w + x] = pixel;
120 dptr += (fbsurf->w - FB_WIDTH) * fbscale;
123 if(SDL_MUSTLOCK(fbsurf)) {
124 SDL_UnlockSurface(fbsurf);
130 static int bnmask(int sdlbn)
133 case SDL_BUTTON_LEFT:
134 return MOUSE_BN_LEFT;
135 case SDL_BUTTON_RIGHT:
136 return MOUSE_BN_RIGHT;
137 case SDL_BUTTON_MIDDLE:
138 return MOUSE_BN_MIDDLE;
146 static void handle_event(SDL_Event *ev)
157 if(ev->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) &&
158 ev->key.state == SDL_PRESSED) {
162 key = sdlkey_to_gamekey(ev->key.keysym.sym, ev->key.keysym.mod);
163 key_event(key, ev->key.state == SDL_PRESSED ? 1 : 0);
167 case SDL_MOUSEMOTION:
168 mouse_x = ev->motion.x / fbscale;
169 mouse_y = ev->motion.y / fbscale;
172 case SDL_MOUSEBUTTONDOWN:
173 mouse_bmask |= bnmask(ev->button.button);
175 case SDL_MOUSEBUTTONUP:
176 mouse_bmask &= ~bnmask(ev->button.button);
178 mouse_x = ev->button.x / fbscale;
179 mouse_y = ev->button.y / fbscale;
188 static void toggle_fullscreen(void)
190 SDL_Surface *newsurf;
191 unsigned int newflags = sdl_flags ^ SDL_FULLSCREEN;
193 if(!(newsurf = SDL_SetVideoMode(xsz, ysz, FB_BPP, newflags))) {
194 fprintf(stderr, "failed to go %s\n", newflags & SDL_FULLSCREEN ? "fullscreen" : "windowed");
199 sdl_flags = newflags;
204 static char symshift[] = {
205 '"', 0, 0, 0, 0, '<', '_', '>', '?',
206 ')', '!', '@', '#', '$', '%', '^', '&', '*', '(',
207 0, ':', 0, '+', 0, 0, 0,
208 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209 '{', '|', '}', 0, 0, '~'
213 static int sdlkey_to_gamekey(int sdlkey, unsigned int mod)
216 if(mod & (KMOD_SHIFT)) {
217 if(sdlkey >= 'a' && sdlkey <= 'z') {
218 sdlkey = toupper(sdlkey);
219 } else if(sdlkey >= SSORG && sdlkey <= SSEND) {
220 sdlkey = symshift[sdlkey - SSORG];
225 if(sdlkey < 256) return 0;