Browse Source

Implement bngviewer utility

rexim 5 years ago
parent
commit
fef4fbed64
5 changed files with 85 additions and 2 deletions
  1. 2 0
      2020-09-04/.gitignore
  2. 7 1
      2020-09-04/Makefile
  3. 1 1
      2020-09-04/README.md
  4. 75 0
      2020-09-04/bngviewer.c
  5. BIN
      2020-09-04/png2bng

+ 2 - 0
2020-09-04/.gitignore

@@ -1,4 +1,6 @@
 a.out
 a.out
 prime
 prime
+png2bng
+bngviewer
 *.wasm
 *.wasm
 *.bng
 *.bng

+ 7 - 1
2020-09-04/Makefile

@@ -1,4 +1,7 @@
-all: prime prime.wasm png2bng tsodinw.bng
+BNGVIEWER_CFLAGS=$(shell pkg-config --cflags sdl2)
+BNGVIEWER_LIBS=$(shell pkg-config --libs sdl2)
+
+all: prime prime.wasm png2bng tsodinw.bng bngviewer
 
 
 prime: prime.c
 prime: prime.c
 	$(CC) -O3 -o prime prime.c
 	$(CC) -O3 -o prime prime.c
@@ -11,3 +14,6 @@ png2bng: png2bng.c bng.h stb_image.h
 
 
 tsodinw.bng: png2bng tsodinw.png
 tsodinw.bng: png2bng tsodinw.png
 	./png2bng
 	./png2bng
+
+bngviewer: bngviewer.c bng.h
+	$(CC) $(BNGVIEWER_CFLAGS) -O3 -o bngviewer bngviewer.c $(BNGVIEWER_LIBS)

+ 1 - 1
2020-09-04/README.md

@@ -32,6 +32,6 @@
 - [ ] Idea: Let's imagine we invented a new image format that didn't exist before
 - [ ] Idea: Let's imagine we invented a new image format that didn't exist before
   - [ ] If we implement the image support in WASM can we make it feel like a native browser support
   - [ ] If we implement the image support in WASM can we make it feel like a native browser support
     - [x] [Implement png2bng](./png2bng.c)
     - [x] [Implement png2bng](./png2bng.c)
-    - [ ] Implement bngviewer
+    - [x] [Implement bngviewer](./bngviewer.c)
     - [ ] Implement bng support in WASM
     - [ ] Implement bng support in WASM
     - [ ] Add more interesting features to bng to test the support
     - [ ] Add more interesting features to bng to test the support

+ 75 - 0
2020-09-04/bngviewer.c

@@ -0,0 +1,75 @@
+#include <assert.h>
+#include <SDL.h>
+#include "bng.h"
+
+void* read_whole_file(const char *filename)
+{
+    FILE *f = fopen(filename, "rb");
+
+    fseek(f, 0, SEEK_END);
+    long size = ftell(f);
+    fseek(f, 0, SEEK_SET);
+    void *data = malloc(size);
+    size_t read_size = fread(data, 1, size, f);
+
+    return data;
+}
+
+struct Bng *bng_load(const char *filepath)
+{
+    return read_whole_file(filepath);
+}
+
+int main(int argc, char *argv[])
+{
+    SDL_Init(SDL_INIT_VIDEO);
+
+    SDL_Window *window =
+        SDL_CreateWindow(
+            "BNG Viewer",
+            0, 0, 800, 600,
+            SDL_WINDOW_RESIZABLE);
+
+
+    SDL_Renderer *renderer =
+        SDL_CreateRenderer(
+                window, -1,
+                SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
+
+    struct Bng *bng = bng_load("tsodinw.bng");
+
+    SDL_Surface* image_surface =
+        SDL_CreateRGBSurfaceFrom(bng->data,
+                                 bng->width,
+                                 bng->height,
+                                 32,
+                                 bng->width * 4,
+                                 0x000000FF,
+                                 0x0000FF00,
+                                 0x00FF0000,
+                                 0xFF000000);
+    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image_surface);
+
+    int quit = 0;
+    while (!quit) {
+        SDL_Event event;
+        while (SDL_PollEvent(&event)) {
+            switch (event.type) {
+            case SDL_QUIT: {
+                quit = 1;
+            } break;
+            }
+        }
+
+        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
+        SDL_RenderClear(renderer);
+
+        SDL_Rect rect = {0, 0, bng->width, bng->height};
+
+        SDL_RenderCopy(renderer, texture, &rect, &rect);
+        SDL_RenderPresent(renderer);
+    }
+
+    SDL_Quit();
+    return 0;
+}

BIN
2020-09-04/png2bng