ob.c (3392B)
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <dlfcn.h> 4 5 #include <SDL2/SDL.h> 6 #include <SDL2/SDL_image.h> 7 8 #define CHECK_DLERROR(ptr) do\ 9 if(!ptr){\ 10 fprintf(stderr, "%s\n", dlerror());\ 11 exit(1);\ 12 } while (0) 13 14 enum keys { 15 LEFT, 16 RIGHT, 17 UP, 18 DOWN, 19 A, 20 B, 21 RETURN, 22 ESCAPE, 23 }; 24 25 /* global data */ 26 unsigned char pixels[256*256]; 27 unsigned char input; 28 29 float FPS; 30 31 void update_FPS(unsigned iter){ 32 static unsigned oldtime; 33 static unsigned newtime; 34 35 if(iter%64 == 0){ 36 oldtime = newtime; 37 newtime = SDL_GetTicks(); 38 FPS = 1000.0/((newtime-oldtime)/64.0); 39 } 40 } 41 42 int get_events(void){ 43 SDL_Event e; 44 while (SDL_PollEvent(&e)) { 45 if (e.type == SDL_QUIT 46 || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_q)) { 47 return 0; 48 } 49 50 if (e.type == SDL_KEYDOWN) { 51 switch (e.key.keysym.sym) { 52 case SDLK_LEFT: 53 input |= 1<<LEFT; 54 break; 55 56 case SDLK_RIGHT: 57 input |= 1<<RIGHT; 58 break; 59 60 case SDLK_UP: 61 input |= 1<<UP; 62 break; 63 64 case SDLK_DOWN: 65 input |= 1<<DOWN; 66 break; 67 68 case SDLK_a: 69 input |= 1<<A; 70 break; 71 72 case SDLK_z: 73 input |= 1<<B; 74 break; 75 76 case SDLK_RETURN: 77 input |= 1<<RETURN; 78 break; 79 80 case SDLK_ESCAPE: 81 input |= 1<<ESCAPE; 82 break; 83 } 84 } 85 if (e.type == SDL_KEYUP) { 86 switch (e.key.keysym.sym) { 87 case SDLK_LEFT: 88 input &= ~(1<<LEFT); 89 break; 90 91 case SDLK_RIGHT: 92 input &= ~(1<<RIGHT); 93 break; 94 95 case SDLK_UP: 96 input &= ~(1<<UP); 97 break; 98 99 case SDLK_DOWN: 100 input &= ~(1<<DOWN); 101 break; 102 103 case SDLK_a: 104 input &= ~(1<<A); 105 break; 106 107 case SDLK_z: 108 input &= ~(1<<B); 109 break; 110 111 case SDLK_RETURN: 112 input &= ~(1<<RETURN); 113 break; 114 115 case SDLK_ESCAPE: 116 input &= ~(1<<ESCAPE); 117 break; 118 } 119 } 120 121 } 122 123 return 1; 124 } 125 126 int main(int argc, char *argv[]){ 127 // init 128 SDL_Init( SDL_INIT_EVERYTHING ); 129 SDL_Window *window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 256, 256, 0); 130 SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); 131 SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING, 256, 256); 132 133 SDL_Rect rect = {.x=0,.y=0,.w=256,.h=256}; 134 135 if(argc != 2){ 136 fprintf(stderr, "Usage: %s ./file.so\n", argv[0]); 137 exit(1); 138 } 139 140 void *handle = dlopen(argv[1], RTLD_LAZY); 141 CHECK_DLERROR(handle); 142 143 void (*init)(unsigned char*, unsigned char*) = dlsym(handle, "init"); 144 CHECK_DLERROR(init); 145 146 void (*loop)() = dlsym(handle, "loop"); 147 CHECK_DLERROR(loop); 148 149 init((unsigned char*)&pixels, &input); 150 151 void *pix; 152 int pitch; 153 SDL_RenderClear(renderer); 154 SDL_LockTexture(texture, &rect, &pix, &pitch); 155 memcpy(pix, pixels, 256*256); 156 SDL_UnlockTexture(texture); 157 158 159 puts("inited"); 160 161 // loop 162 unsigned iter = 0; 163 int cont = 1; 164 while(cont) { 165 update_FPS(iter); 166 167 cont = get_events(); 168 169 fprintf(stderr,"input %3u, FPS %3.3f\r", (unsigned) input, FPS); 170 171 loop(); 172 173 SDL_LockTexture(texture, &rect, &pix, &pitch); 174 memcpy(pix, pixels, 256*256); 175 SDL_UnlockTexture(texture); 176 177 SDL_RenderCopy(renderer, texture, &rect, &rect); 178 SDL_RenderPresent(renderer); 179 SDL_RenderClear(renderer); 180 181 iter++; 182 } 183 184 // finish 185 puts("\nfinish"); 186 187 SDL_DestroyTexture(texture); 188 SDL_DestroyRenderer(renderer); 189 SDL_DestroyWindow(window); 190 SDL_Quit(); 191 192 return 0; 193 }