/* game.c - Main game loop. */ #include #include #include #include #include #include #include #include "defs.h" #include "event.h" #include "video.h" Uint32 palette[256]; void load_palette(char* palfile, SDL_bool vga) { struct stat st; FILE *f; int i; stat(palfile, &st); if(errno) { printf("unable to stat file: %s\n", strerror(errno)); exit(1); } if(st.st_size != 768) { printf("invalid size (expected 768 byes, got %li bytes)\n", st.st_size); exit(1); } f = fopen(palfile, "rb"); if(f == NULL) { printf("unable to open file: %s\n", strerror(errno)); exit(1); } for(i = 0; i < 256; i++) { Uint8 r, g, b; r = (Uint8)fgetc(f); g = (Uint8)fgetc(f); b = (Uint8)fgetc(f); if(!vga) palette[i] = RGB(r, g, b); else palette[i] = RGB(r * 4, g * 4, b * 4); } fclose(f); } void game_loop(char *palfile, SDL_bool vga) { event_result_t ev = EV_NONE; SDL_bool running = SDL_TRUE; int cx, cy; load_palette(palfile, vga); if(video_init(PROJECT_NAME, RENDER_WIDTH, RENDER_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT) != 0) { printf("error initializing video\n"); exit(1); } /* Clear the framebuffer. */ memset(pixels, 0x00, RENDER_WIDTH * RENDER_HEIGHT * 4); /* Plot things! */ for(cy = 0; cy < 16; cy++) { for(cx = 0; cx < 16; cx++) { int i = (cy * 16) + cx; int c = palette[i]; int sx, sy; for(sy = BOX_SPACING; sy < BOX_SIZE; sy++) { for(sx = BOX_SPACING; sx < BOX_SIZE; sx++) { int x = (BOX_SIZE) * cx + sx; int y = (BOX_SIZE) * cy + sy; pixels[POS(x, y)] = c; } } } } while(running) { ev = event_process(); switch(ev) { case EV_QUIT: running = SDL_FALSE; break; } video_update(); } }