ERR_error WPR_init_sdl(WPR_init_sdl_data init,MEM_heap_manager* manager){ WPR_sdl_data* data = MEM_get_item_m_p(WPR_sdl_data,MEM_get_heap_m(manager,MEM_LOC_WPR_SDL_DATA),0); int i,j; if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0){ SDL_LogError(SDL_LOG_CATEGORY_ERROR,"Couldn't init SDL: %s",SDL_GetError()); return ERR_BAD; } data->window = SDL_CreateWindow(init.title,SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,init.screen_width,init.screen_height,SDL_WINDOW_SHOWN); if(!data->window){ SDL_LogError(SDL_LOG_CATEGORY_ERROR,"Couldn't create a window: %s",SDL_GetError()); return ERR_BAD; } data->renderer = SDL_CreateRenderer(data->window,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if(!data->renderer){ SDL_LogError(SDL_LOG_CATEGORY_ERROR,"Couldn't create a renderer: %s",SDL_GetError()); return ERR_BAD; } ERR_ASSERT(WPR_get_status() == WPR_READY,"SDL wrapper not inited"); int result = IMG_Init(IMG_INIT_PNG); ERR_ASSERT((result == IMG_INIT_PNG),"Couldn't init SDL_image: %s",IMG_GetError()); data->screen_width = init.screen_width; data->screen_height = init.screen_height; data->unit_x = init.screen_width/IDEAL_WIDTH; data->unit_y = -init.screen_height/IDEAL_HEIGHT; data->scale_x = data->unit_x / (IDEAL_UNIT_X); data->scale_y = -data->unit_y / (IDEAL_UNIT_Y); data->offset_x = (IDEAL_WIDTH/2); data->offset_y = -(IDEAL_HEIGHT/2); data->running = 1; data->delta_time = 1; for(i=0;i<DELTA_SAMPLE_SIZE;i++){ data->delta_time_data[i] = 1; } data->delta_collection_count = 0; data->current_fps = 0; data->prev_tick = SDL_GetTicks(); data->textures = MEM_get_heap_m(manager,MEM_LOC_TEXTURE_DATA); WPR_STATUS = WPR_READY; return ERR_GOOD; }
ERR_error WPR_shutdown(WPR_sdl_data* data){ ERR_ASSERT(AST_get_status() == AST_CLOSED,"assets not closed yet"); SDL_DestroyRenderer(data->renderer); SDL_DestroyWindow(data->window); data->renderer = NULL; data->window = NULL; IMG_Quit(); SDL_Quit(); WPR_STATUS = WPR_CLOSED; return ERR_GOOD; }
ERR_error AST_init(MEM_heap_manager* manager){ ERR_ASSERT(WPR_get_status() == WPR_READY,"sdl_wrapper not ready"); MEM_handle handle = MEM_create_handle(MEM_LOC_AST_DATA,0); AST_data* data = MEM_get_item_p(AST_data,handle,manager); data->audio_data = MEM_get_heap_m(manager,MEM_LOC_AUDIO_DATA); data->texture_data = MEM_get_heap_m(manager,MEM_LOC_TEXTURE_DATA); data->sdl_data = MEM_get_item_m_p(WPR_sdl_data,MEM_get_heap_m(manager,MEM_LOC_WPR_SDL_DATA),0); find_all_and_load_textures(data); AST_STATUS = AST_READY; return ERR_BAD; }
/* * Source: http://zarb.org/~gc/html/libpng.html */ int save_image_png(image_t *image, char* file_name) { int ret = 0; if (image == NULL || file_name == NULL) goto error; int width = image->width; int height = image->height; /* create file */ FILE *fp = fopen(file_name, "wb"); ERR_ASSERT(fp, "Failed to open output file"); /* initialize stuff */ png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); ERR_ASSERT(png_ptr, "[write_png_file] png_create_write_struct failed"); png_infop info_ptr = png_create_info_struct(png_ptr); ERR_ASSERT(info_ptr, "[write_png_file] png_create_info_struct failed"); ERR_ASSERT(!setjmp(png_jmpbuf(png_ptr)), "[write_png_file] Error during init_io"); png_init_io(png_ptr, fp); /* write header */ ERR_ASSERT(!setjmp(png_jmpbuf(png_ptr)), "[write_png_file] Error during writing header"); png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); png_write_info(png_ptr, info_ptr); /* write bytes */ ERR_ASSERT(!setjmp(png_jmpbuf(png_ptr)), "[write_png_file] Error during writing bytes"); png_write_image(png_ptr, image->rows); /* end write */ ERR_ASSERT(!setjmp(png_jmpbuf(png_ptr)), "[write_png_file] Error during end of write"); png_write_end(png_ptr, NULL); fclose(fp); done: return ret; error: ret = -1; goto done; }
LIB_HANDLE AST_lib_open(const char* str){ LIB_HANDLE handle = dlopen(str,RTLD_LAZY); ERR_ASSERT(handle != NULL,"could not open lib at %s",str); return handle; }