From 0e1a6d0a7ae5053a70c2e35e18ac3713528f0870 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Thu, 5 Nov 2015 20:34:05 +0000 Subject: Add image loading stub --- loader.c | 29 +++++++++++++++++++++++++++++ main.c | 18 +++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 loader.c diff --git a/loader.c b/loader.c new file mode 100644 index 0000000..0a94cf9 --- /dev/null +++ b/loader.c @@ -0,0 +1,29 @@ +#include +#include + +SDL_Texture* imv_load_image(SDL_Renderer *r, const char* path) +{ + if(!path) + return NULL; + + const char* ext = strrchr(path, '.'); + SDL_Texture *img = NULL; + if(ext == NULL) { + fprintf(stderr, + "Could not determine filetype of '%s' from its extension.\n", + path); + } else if(strcasecmp(ext, ".test") == 0) { + const int width = 1280; + const int height = 720; + img = SDL_CreateTexture(r, + SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_STATIC, + width, height); + void *pixels = malloc(width*height*3); + memset(pixels, 255, width*height*3); + SDL_Rect area = {0,0,width,height}; + SDL_UpdateTexture(img, &area, pixels, width * 3); + free(pixels); + } + + return img; +} diff --git a/main.c b/main.c index 55dce77..b4fae06 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,8 @@ #include #include +SDL_Texture* imv_load_image(SDL_Renderer *r, const char* path); + int main(int argc, char** argv) { if(argc < 2) { @@ -30,6 +32,7 @@ int main(int argc, char** argv) const int num_paths = argc - 1; int cur_path = -1; int next_path = 0; + SDL_Texture *img = NULL; int quit = 0; int redraw = 1; @@ -69,12 +72,21 @@ int main(int argc, char** argv) if(next_path != cur_path) { cur_path = next_path; fprintf(stdout, "current image: %s\n", paths[cur_path]); + if(img) { + SDL_DestroyTexture(img); + img = NULL; + } + img = imv_load_image(renderer, paths[cur_path]); redraw = 1; } if(redraw) { SDL_RenderClear(renderer); - //SDL_RenderCopy(renderer, tex, srcrect, dstrect); + + if(img) { + SDL_Rect area = {0,0,width,height}; + SDL_RenderCopy(renderer, img, &area, &area); + } SDL_RenderPresent(renderer); redraw = 0; @@ -82,6 +94,10 @@ int main(int argc, char** argv) SDL_Delay(10); } + if(img) { + SDL_DestroyTexture(img); + img = NULL; + } SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); -- cgit v1.2.3