gba-x3dtest

annotate src/main_sdl.c @ 6:73b5f2e5d18a

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