diff options
author | Harry Jeffery <harry@exec64.co.uk> | 2015-11-05 20:34:05 +0000 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2015-11-05 20:43:31 +0000 |
commit | 0e1a6d0a7ae5053a70c2e35e18ac3713528f0870 (patch) | |
tree | fcbc8bd839416ef28e8350f4ccf90b36f711bcf9 | |
parent | 0639d83b2d79e2c03c43cdc85e1cb28630ed14bb (diff) | |
download | imv-0e1a6d0a7ae5053a70c2e35e18ac3713528f0870.tar.gz |
Add image loading stub
-rw-r--r-- | loader.c | 29 | ||||
-rw-r--r-- | main.c | 18 |
2 files changed, 46 insertions, 1 deletions
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 <SDL2/SDL.h> +#include <string.h> + +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; +} @@ -1,6 +1,8 @@ #include <stdio.h> #include <SDL2/SDL.h> +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(); |