gba-trycatch

annotate src/main_sdl.c @ 5:850be43b3135

sdl version
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 16 Jun 2014 22:01:45 +0300
parents
children 73b5f2e5d18a
rev   line source
nuclear@5 1 #include "config.h"
nuclear@5 2 #include <stdio.h>
nuclear@5 3 #include <stdlib.h>
nuclear@5 4 #include <SDL.h>
nuclear@5 5 #include "gbasys.h"
nuclear@5 6 #include "game.h"
nuclear@5 7
nuclear@5 8 static int proc_events(SDL_Event *ev);
nuclear@5 9 static void handle_keyboard(int key, int state);
nuclear@5 10
nuclear@5 11 struct pixel_buffer *back_buffer, *front_buffer;
nuclear@5 12
nuclear@5 13 static SDL_Surface *surf;
nuclear@5 14 static struct pixel_buffer bbuf;
nuclear@5 15 static unsigned int keystate;
nuclear@5 16
nuclear@5 17 int main(void)
nuclear@5 18 {
nuclear@5 19 int i, j;
nuclear@5 20
nuclear@5 21 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
nuclear@5 22
nuclear@5 23 if(!(surf = SDL_SetVideoMode(WIDTH * SDLSCALE, HEIGHT * SDLSCALE, 16, SDL_SWSURFACE))) {
nuclear@5 24 fprintf(stderr, "failed to initialize graphics\n");
nuclear@5 25 return 1;
nuclear@5 26 }
nuclear@5 27 SDL_WM_SetCaption("trycatch", 0);
nuclear@5 28
nuclear@5 29 bbuf.x = WIDTH;
nuclear@5 30 bbuf.y = HEIGHT;
nuclear@5 31 bbuf.bpp = 16;
nuclear@5 32 if(!(bbuf.pixels = malloc(WIDTH * HEIGHT * 2))) {
nuclear@5 33 fprintf(stderr, "failed to allocate framebuffer (%dx%d)\n", WIDTH, HEIGHT);
nuclear@5 34 SDL_Quit();
nuclear@5 35 return 1;
nuclear@5 36 }
nuclear@5 37
nuclear@5 38 back_buffer = front_buffer = &bbuf;
nuclear@5 39
nuclear@5 40 for(;;) {
nuclear@5 41 SDL_Event ev;
nuclear@5 42 uint16_t *dest, *src;
nuclear@5 43
nuclear@5 44 while(SDL_PollEvent(&ev)) {
nuclear@5 45 if(proc_events(&ev) == -1) {
nuclear@5 46 goto done;
nuclear@5 47 }
nuclear@5 48 }
nuclear@5 49
nuclear@5 50 game_draw();
nuclear@5 51
nuclear@5 52 if(SDL_MUSTLOCK(surf)) {
nuclear@5 53 SDL_LockSurface(surf);
nuclear@5 54 }
nuclear@5 55
nuclear@5 56 src = bbuf.pixels;
nuclear@5 57 dest = surf->pixels;
nuclear@5 58
nuclear@5 59 for(i=0; i<HEIGHT; i++) {
nuclear@5 60 for(j=0; j<WIDTH; j++) {
nuclear@5 61 dest[0] = dest[1] = dest[WIDTH] = dest[WIDTH + 1] = *src++;
nuclear@5 62 dest += 2;
nuclear@5 63 }
nuclear@5 64 dest += WIDTH;
nuclear@5 65 }
nuclear@5 66
nuclear@5 67 if(SDL_MUSTLOCK(surf)) {
nuclear@5 68 SDL_UnlockSurface(surf);
nuclear@5 69 }
nuclear@5 70 keystate = 0;
nuclear@5 71 }
nuclear@5 72
nuclear@5 73 done:
nuclear@5 74 SDL_Quit();
nuclear@5 75 return 0;
nuclear@5 76 }
nuclear@5 77
nuclear@5 78 void flip(void)
nuclear@5 79 {
nuclear@5 80 SDL_Flip(surf);
nuclear@5 81 }
nuclear@5 82
nuclear@5 83 void clear_buffer(struct pixel_buffer *pbuf, unsigned short color)
nuclear@5 84 {
nuclear@5 85 int i;
nuclear@5 86 unsigned short *pixels = pbuf->pixels;
nuclear@5 87
nuclear@5 88 for(i=0; i<pbuf->x * pbuf->y; i++) {
nuclear@5 89 *pixels++ = color;
nuclear@5 90 }
nuclear@5 91 }
nuclear@5 92
nuclear@5 93 int get_key_state(int key)
nuclear@5 94 {
nuclear@5 95 return keystate & key;
nuclear@5 96 }
nuclear@5 97
nuclear@5 98 unsigned long get_millisec(void)
nuclear@5 99 {
nuclear@5 100 return SDL_GetTicks();
nuclear@5 101 }
nuclear@5 102
nuclear@5 103 static int proc_events(SDL_Event *ev)
nuclear@5 104 {
nuclear@5 105 switch(ev->type) {
nuclear@5 106 case SDL_KEYDOWN:
nuclear@5 107 case SDL_KEYUP:
nuclear@5 108 handle_keyboard(ev->key.keysym.sym, ev->key.state == SDL_KEYDOWN);
nuclear@5 109 break;
nuclear@5 110
nuclear@5 111 case SDL_QUIT:
nuclear@5 112 return -1;
nuclear@5 113 }
nuclear@5 114
nuclear@5 115 return 0;
nuclear@5 116 }
nuclear@5 117
nuclear@5 118 static void handle_keyboard(int key, int state)
nuclear@5 119 {
nuclear@5 120 int gba_key = -1;
nuclear@5 121
nuclear@5 122 switch(key) {
nuclear@5 123 case SDLK_ESCAPE:
nuclear@5 124 {
nuclear@5 125 SDL_Event ev;
nuclear@5 126 ev.type = SDL_QUIT;
nuclear@5 127 SDL_PushEvent(&ev);
nuclear@5 128 }
nuclear@5 129 break;
nuclear@5 130
nuclear@5 131 case 'a':
nuclear@5 132 case 'A':
nuclear@5 133 case SDLK_LEFT:
nuclear@5 134 gba_key = KEY_LEFT;
nuclear@5 135 break;
nuclear@5 136
nuclear@5 137 case 'd':
nuclear@5 138 case 'D':
nuclear@5 139 case SDLK_RIGHT:
nuclear@5 140 gba_key = KEY_RIGHT;
nuclear@5 141 break;
nuclear@5 142
nuclear@5 143 case 'w':
nuclear@5 144 case 'W':
nuclear@5 145 case SDLK_UP:
nuclear@5 146 gba_key = KEY_UP;
nuclear@5 147 break;
nuclear@5 148
nuclear@5 149 case 's':
nuclear@5 150 case 'S':
nuclear@5 151 case SDLK_DOWN:
nuclear@5 152 gba_key = KEY_DOWN;
nuclear@5 153 break;
nuclear@5 154
nuclear@5 155 case '\n':
nuclear@5 156 gba_key = KEY_A;
nuclear@5 157 break;
nuclear@5 158 case '\b':
nuclear@5 159 gba_key = KEY_B;
nuclear@5 160 break;
nuclear@5 161
nuclear@5 162 case ',':
nuclear@5 163 gba_key = KEY_SELECT;
nuclear@5 164 break;
nuclear@5 165 case '.':
nuclear@5 166 gba_key = KEY_START;
nuclear@5 167 break;
nuclear@5 168
nuclear@5 169 case '[':
nuclear@5 170 gba_key = KEY_L;
nuclear@5 171 break;
nuclear@5 172 case ']':
nuclear@5 173 gba_key = KEY_R;
nuclear@5 174 break;
nuclear@5 175
nuclear@5 176 default:
nuclear@5 177 return;
nuclear@5 178 }
nuclear@5 179
nuclear@5 180 keystate |= gba_key;
nuclear@5 181 game_keyb(gba_key, state);
nuclear@5 182 }