Browse Source

Merge pull request #19 from tsoding/bake-digits-png

Bake digits.png into the executable during the build
Alexey Kutepov 4 years ago
parent
commit
3556deb6d3
5 changed files with 77 additions and 22 deletions
  1. 3 0
      .gitignore
  2. 11 3
      Makefile
  3. 3 1
      build_msvc.bat
  4. 9 18
      main.c
  5. 51 0
      png2c.c

+ 3 - 0
.gitignore

@@ -1,6 +1,9 @@
 sowon
+png2c
+digits.h
 *.kra
 *~
 SDL2
 *.obj
 *.exe
+.dSYM/

+ 11 - 3
Makefile

@@ -1,5 +1,13 @@
-CFLAGS=`pkg-config --cflags sdl2` -Wall -Wextra -std=c99 -pedantic
-LIBS=`pkg-config --libs sdl2` -lm
+COMMON_CFLAGS=-Wall -Wextra -std=c99 -pedantic
+CFLAGS=`pkg-config --cflags sdl2` $(COMMON_CFLAGS)
+COMMON_LIBS=-lm
+LIBS=`pkg-config --libs sdl2` $(COMMON_LIBS)
 
-sowon: main.c
+sowon: main.c digits.h
 	$(CC) $(CFLAGS) -o sowon main.c $(LIBS)
+
+digits.h: png2c digits.png
+	./png2c digits.png > digits.h
+
+png2c: png2c.c
+	$(CC) $(COMMON_CFLAGS) -o png2c png2c.c -lm

+ 3 - 1
build_msvc.bat

@@ -5,4 +5,6 @@ set CXXFLAGS=/std:c++17 /O2 /FC /W4 /WX /nologo
 set INCLUDES=/I SDL2\include
 set LIBS=SDL2\lib\x64\SDL2.lib SDL2\lib\x64\SDL2main.lib Shell32.lib
 
-cl.exe %CXXFLAGS% %INCLUDES% /Fesowon main.c /link %LIBS% -SUBSYSTEM:windows
+cl.exe %CXXFLAGS% /Fepng2c png2c.c /link Shell32.lib -SUBSYSTEM:console
+png2c.exe digits.png > digits.h
+cl.exe %CXXFLAGS% %INCLUDES% /Fesowon main.c /link %LIBS% -SUBSYSTEM:windows

+ 9 - 18
main.c

@@ -6,8 +6,7 @@
 
 #include <SDL.h>
 
-#define STB_IMAGE_IMPLEMENTATION
-#include "stb_image.h"
+#include "./digits.h"
 
 #define FPS 60
 #define DELTA_TIME (1.0f / FPS)
@@ -50,22 +49,15 @@ void *secp(void *ptr)
     return ptr;
 }
 
-SDL_Surface *load_png_file_as_surface(const char *image_filename)
+SDL_Surface *load_png_file_as_surface()
 {
-    int width, height;
-    uint32_t *image_pixels = (uint32_t *) stbi_load(image_filename, &width, &height, NULL, 4);
-    if (image_pixels == NULL) {
-        fprintf(stderr, "[ERROR] Could not load `%s` as PNG\n", image_filename);
-        abort();
-    }
-
     SDL_Surface* image_surface =
         secp(SDL_CreateRGBSurfaceFrom(
-                 image_pixels,
-                 (int) width,
-                 (int) height,
+                 png,
+                 (int) png_width,
+                 (int) png_height,
                  32,
-                 (int) width * 4,
+                 (int) png_width * 4,
                  0x000000FF,
                  0x0000FF00,
                  0x00FF0000,
@@ -73,10 +65,9 @@ SDL_Surface *load_png_file_as_surface(const char *image_filename)
     return image_surface;
 }
 
-SDL_Texture *load_png_file_as_texture(SDL_Renderer *renderer,
-                                      const char *image_filename)
+SDL_Texture *load_png_file_as_texture(SDL_Renderer *renderer)
 {
-    SDL_Surface *image_surface = load_png_file_as_surface(image_filename);
+    SDL_Surface *image_surface = load_png_file_as_surface();
     return secp(SDL_CreateTextureFromSurface(renderer, image_surface));
 }
 
@@ -189,7 +180,7 @@ int main(int argc, char **argv)
 
     secc(SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"));
 
-    SDL_Texture *digits = load_png_file_as_texture(renderer, "./digits.png");
+    SDL_Texture *digits = load_png_file_as_texture(renderer);
     secc(SDL_SetTextureColorMod(digits, MAIN_COLOR_R, MAIN_COLOR_G, MAIN_COLOR_B));
 
     if (paused) {

+ 51 - 0
png2c.c

@@ -0,0 +1,51 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#define STB_IMAGE_IMPLEMENTATION
+#include "./stb_image.h"
+
+const char *shift(int *argc, char ***argv)
+{
+    assert(*argc > 0);
+    const char *result = *argv[0];
+    *argc -= 1;
+    *argv += 1;
+    return result;
+}
+
+int main(int argc, char *argv[])
+{
+    shift(&argc, &argv);        // skip program name
+
+    if (argc <= 0) {
+        fprintf(stderr, "Usage: png2c <filepath.png>\n");
+        fprintf(stderr, "ERROR: expected file path\n");
+        exit(1);
+    }
+
+    const char *filepath = shift(&argc, &argv);
+
+    int x, y, n;
+    uint32_t *data = (uint32_t *)stbi_load(filepath, &x, &y, &n, 4);
+
+    if (data == NULL) {
+        fprintf(stderr, "Could not load file `%s`\n", filepath);
+        exit(1);
+    }
+
+    // TODO: inclusion guards and the array name are not customizable
+    printf("#ifndef PNG_H_\n");
+    printf("#define PNG_H_\n");
+    printf("size_t png_width = %d;\n", x);
+    printf("size_t png_height = %d;\n", y);
+    printf("uint32_t png[] = {");
+    for (size_t i = 0; i < (size_t)(x * y); ++i) {
+        printf("0x%x, ", data[i]);
+    }
+    printf("};\n");
+    printf("#endif // PNG_H_\n");
+
+    return 0;
+}