Experimento con emscripten y SDL
#include "engine.h"1#include <SDL2/SDL.h>2#include <SDL2/SDL_image.h>34void setup_textures();5void setup_decals();6void setup_actors();7void loop_handler();8bool should_continue_logic_loops();9void setup_bindings();1011engine *engine_init(12unsigned int w,13unsigned int h)14{15eng.fps = 80;16eng.current_frame = 0;17eng.score = 0;1819eng.start_time = 0;20eng.should_start_logic_loop = true;21eng.whole_frames_to_do = 0;2223SDL_Init(SDL_INIT_VIDEO);24TTF_Init();2526SDL_CreateWindowAndRenderer(w, h, 0, &eng.window, &eng.renderer);27eng.w = w;28eng.h = h;2930if(eng.window == NULL) {31fprintf(32stderr,33"Window could not be created: %s\n", SDL_GetError());34return NULL;35}3637setup_textures();38setup_decals();3940eng.grid_width = w / TILE_DIMENSION;41eng.grid_height = (h / TILE_DIMENSION) - STATUS_BAR_HEIGHT;42eng.occupied_gridpoints = malloc(eng.grid_width * eng.grid_height * sizeof(*eng.occupied_gridpoints));43memset(eng.occupied_gridpoints, 0, eng.grid_width * eng.grid_height * sizeof(*eng.occupied_gridpoints));4445SDL_StartTextInput();46setup_bindings();4748eng.render_list = NULL;49eng.logic_list = NULL;5051setup_actors();5253return ŋ54}5556void engine_destroy()57{58SDL_Quit();59}6061void engine_start()62{63eng.start_time = SDL_GetTicks();6465emscripten_set_main_loop(loop_handler, -1, 0);66}6768void engine_reset()69{70snake_actor_init(&eng.snake_actor);71memset(eng.occupied_gridpoints, 0, eng.grid_width * eng.grid_height * sizeof(*eng.occupied_gridpoints));72apple_actor_init(&eng.apple_actor);73eng.score = 0;74background_actor_update_scoreboard(&eng.background_actor);75}7677void setup_textures()78{79char * filenames[] = {80"assets/sand.png",81"assets/sprites.png",82"assets/status_bar.png"83};84int i;8586for (i = 0; i < sizeof(filenames) / sizeof(filenames[0]); i++) {87char * fname = filenames[i];88SDL_Surface *img = IMG_Load(fname);8990if(!img){91fprintf(stdout, "Error! Could not load %s\n", fname);92exit(1);93}9495eng.textures[i] = SDL_CreateTextureFromSurface(eng.renderer, img);9697SDL_FreeSurface(img);98}99}100101void setup_decals()102{103int i;104105int imgw, imgh;106SDL_QueryTexture(eng.textures[SPRITES_TEXTURE],107NULL, NULL, &imgw, &imgh);108109for (i = 0; i < NUM_SPRITES_DECALS; i++) {110double w = 1/8. * imgw;111double h = 1/2. * imgh;112double x = (double)(i % 8) * w;113double y = (double)(i / 8) * h;114115decal_init(116&eng.sprites_decals[i],117eng.textures[SPRITES_TEXTURE],118x,119y,120w,121h);122}123124SDL_QueryTexture(eng.textures[SAND_TEXTURE],125NULL, NULL, &imgw, &imgh);126127decal_init(128&(eng.sand_decal),129eng.textures[SAND_TEXTURE],1300,1310,132imgw,133imgh);134}135136void setup_actors()137{138snake_actor_init(&eng.snake_actor);139eng.render_list = actor_list_add(eng.render_list, (actor *)(&eng.snake_actor));140eng.logic_list = actor_list_add(eng.logic_list, (actor *)(&eng.snake_actor));141142apple_actor_init(&eng.apple_actor);143eng.render_list = actor_list_add(eng.render_list, (actor *)(&eng.apple_actor));144145background_actor_init(&eng.background_actor);146eng.render_list = actor_list_add(eng.render_list, (actor *)(&eng.background_actor));147}148149bool should_continue_logic_loops()150{151if (eng.should_start_logic_loop) {152unsigned int logic_loop_start_time = SDL_GetTicks();153double elapsed_frames = (double)(logic_loop_start_time \154- eng.start_time) / 1000.0f * eng.fps;155156eng.whole_frames_to_do = (unsigned int)elapsed_frames - eng.current_frame;157}158159if (!eng.whole_frames_to_do) {160eng.should_start_logic_loop = true;161return false;162}163164eng.whole_frames_to_do -= 1;165eng.current_frame += 1;166eng.should_start_logic_loop = false;167return true;168}169170void loop_handler()171{172process_input();173174// if (is_state_active(GS_QUIT)) {175// return;176// }177178SDL_RenderClear(eng.renderer);179180actor_list *al;181for (al = eng.render_list; al != NULL; al = al->next) {182al->a->render_handler(al->a);183}184185while (should_continue_logic_loops()) {186for (al = eng.logic_list; al != NULL; al = al->next) {187al->a->logic_handler(al->a);188}189}190191SDL_RenderPresent(eng.renderer);192}193194void setup_bindings()195{196input_processor_init();197key_state_binding binding;198199binding.k = SDLK_UP;200binding.s = GS_N;201binding.t = BINDING_ONE_TIME;202add_binding(&binding);203204binding.k = SDLK_RIGHT;205binding.s = GS_E;206add_binding(&binding);207208binding.k = SDLK_DOWN;209binding.s = GS_S;210add_binding(&binding);211212binding.k = SDLK_LEFT;213binding.s = GS_W;214add_binding(&binding);215}216217void engine_get_grid_coord(const int *pixel_coord, int *grid_coord)218{219grid_coord[0] = pixel_coord[0] / TILE_DIMENSION;220grid_coord[1] = pixel_coord[1] / TILE_DIMENSION - STATUS_BAR_HEIGHT;221}222223void engine_get_pixel_coord(const int *grid_coord, int *pixel_coord)224{225pixel_coord[0] = grid_coord[0] * TILE_DIMENSION;226pixel_coord[1] = (grid_coord[1] + STATUS_BAR_HEIGHT) * TILE_DIMENSION;227}228229void engine_apply_boundary_conditions(int *grid_coords)230{231grid_coords[0] = ((grid_coords[0] % eng.grid_width) + eng.grid_width) % eng.grid_width;232grid_coords[1] = ((grid_coords[1] % eng.grid_height) + eng.grid_height) % eng.grid_height;233}234235236