fractal effect is cooking
[dosdemo] / src / sdl / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <SDL/SDL.h>
5 #include "demo.h"
6
7 static void handle_event(SDL_Event *ev);
8
9 static int quit;
10 static long start_time;
11 static SDL_Surface *fbsurf;
12
13 static int fbscale = 2;
14 static int xsz, ysz;
15
16 int main(int argc, char **argv)
17 {
18         int s, i, j;
19         char *env;
20         unsigned short *sptr, *dptr;
21         unsigned int sdl_flags = SDL_SWSURFACE;
22
23         if((env = getenv("FBSCALE")) && (s = atoi(env))) {
24                 fbscale = s;
25                 printf("Framebuffer scaling x%d\n", fbscale);
26         }
27
28         xsz = fb_width * fbscale;
29         ysz = fb_height * fbscale;
30
31         if(!(fb_pixels = malloc(fb_width * fb_height * fb_bpp / CHAR_BIT))) {
32                 fprintf(stderr, "failed to allocate virtual framebuffer\n");
33                 return 1;
34         }
35
36         SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
37         if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, sdl_flags))) {
38                 fprintf(stderr, "failed to set video mode %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
39                 free(fb_pixels);
40                 SDL_Quit();
41                 return 1;
42         }
43         SDL_WM_SetCaption("dosdemo/SDL", 0);
44
45         time_msec = 0;
46         if(demo_init(argc, argv) == -1) {
47                 free(fb_pixels);
48                 SDL_Quit();
49                 return 1;
50         }
51         start_time = SDL_GetTicks();
52
53         while(!quit) {
54                 SDL_Event ev;
55                 while(SDL_PollEvent(&ev)) {
56                         handle_event(&ev);
57                         if(quit) goto break_evloop;
58                 }
59
60                 time_msec = SDL_GetTicks() - start_time;
61                 demo_draw();
62
63                 if(SDL_MUSTLOCK(fbsurf)) {
64                         SDL_LockSurface(fbsurf);
65                 }
66
67                 sptr = fb_pixels;
68                 dptr = fbsurf->pixels;
69                 for(i=0; i<fb_height; i++) {
70                         for(j=0; j<fb_width; j++) {
71                                 int x, y;
72                                 unsigned short pixel = *sptr++;
73
74                                 for(y=0; y<fbscale; y++) {
75                                         for(x=0; x<fbscale; x++) {
76                                                 dptr[y * xsz + x] = pixel;
77                                         }
78                                 }
79                                 dptr += fbscale;
80                         }
81                         dptr += xsz * (fbscale - 1);
82                 }
83
84                 if(SDL_MUSTLOCK(fbsurf)) {
85                         SDL_UnlockSurface(fbsurf);
86                 }
87                 SDL_Flip(fbsurf);
88         }
89
90 break_evloop:
91         demo_cleanup();
92         SDL_Quit();
93         return 0;
94 }
95
96 void demo_quit(void)
97 {
98         quit = 1;
99 }
100
101 static void handle_event(SDL_Event *ev)
102 {
103         switch(ev->type) {
104         case SDL_QUIT:
105                 quit = 1;
106                 break;
107
108         case SDL_KEYDOWN:
109         case SDL_KEYUP:
110                 demo_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0);
111                 break;
112
113         case SDL_MOUSEMOTION:
114                 mouse_x = ev->motion.x / fbscale;
115                 mouse_y = ev->motion.y / fbscale;
116                 break;
117
118         case SDL_MOUSEBUTTONDOWN:
119                 mouse_bmask |= 1 << ev->button.button;
120                 if(0) {
121         case SDL_MOUSEBUTTONUP:
122                         mouse_bmask &= ~(1 << ev->button.button);
123                 }
124                 mouse_x = ev->button.x / fbscale;
125                 mouse_y = ev->button.y / fbscale;
126                 break;
127
128         default:
129                 break;
130         }
131 }