Make alpha checker texture from code
dkl9

dkl9 commited on 2026-116 14:16:09
Showing 3 changed files, with 27 additions and 18 deletions.

... ...
@@ -4,7 +4,7 @@ To compile:
4 4
 make iv
5 5
 
6 6
 To install, with $INSTALL_DIR somewhere in your $PATH:
7
-cp iv alpha_checker.bmp $INSTALL_DIR
7
+cp iv $INSTALL_DIR
8 8
 
9 9
 To make iv the default, if on Linux:
10 10
 ULSA=/usr/local/share/applications
... ...
@@ -39,23 +39,32 @@ void cfail(const int cond, const char* msg, const int showsdl) {
39 39
     }
40 40
 }
41 41
 
42
-/* load and return alpha checkers, relative to iv binary */
43
-SDL_Texture* checker(SDL_Renderer* ren, char* progname, int* w, int* h) {
44
-    char* tpath = malloc(strlen(progname) + 32);
45
-    cfail(!tpath, "allocate memory", 0);
46
-    sprintf(tpath, "%s/alpha_checker.bmp", dirname(progname));
47
-    SDL_Surface* acs = SDL_LoadBMP(tpath);
48
-    if (w) {
49
-        *w = acs->w;
42
+/* make alpha checker pattern */
43
+SDL_Texture* checker(SDL_Renderer* ren, int w, int h) {
44
+    SDL_Texture* tex = SDL_CreateTexture(
45
+        ren,
46
+        SDL_PIXELFORMAT_RGBA8888,
47
+        SDL_TEXTUREACCESS_STREAMING,
48
+        w, h
49
+    );
50
+    cfail(!tex, "make background texture", 1);
51
+
52
+    void* pixels;
53
+    int pitch;
54
+    cfail(
55
+        SDL_LockTexture(tex, NULL, &pixels, &pitch),
56
+        "lock background texture", 1
57
+    );
58
+
59
+    for (int y = 0; y < h; ++y) {
60
+        Uint32* row = (Uint32*)((Uint8*)pixels + y * pitch);
61
+        for (int x = 0; x < w; ++x) {
62
+            row[x] = 0xB0B0B0FF + 0x40404000 * (x + y & 1);
50 63
         }
51
-    if (h) {
52
-        *h = acs->h;
53 64
     }
54
-    cfail(!acs, "load alpha checker texture", 1);
55
-    SDL_Texture* act = SDL_CreateTextureFromSurface(ren, acs);
56
-    cfail(!act, "create surface", 1);
57
-    SDL_FreeSurface(acs);
58
-    return act;
65
+
66
+    SDL_UnlockTexture(tex);
67
+    return tex;
59 68
 }
60 69
 
61 70
 /* process command-line options, setting pointed strings */
... ...
@@ -206,8 +215,8 @@ int main(int argc, char** argv) {
206 215
     cfail(!ren, "create renderer", 1);
207 216
 
208 217
     /* make images into textures */
209
-    int cw, ch;
210
-    SDL_Texture* act = checker(ren, argv[0], &cw, &ch);
218
+    int cw = 64, ch = 64;
219
+    SDL_Texture* act = checker(ren, cw, ch);
211 220
     struct ViewState* sl = calloc(fc, sizeof(struct ViewState));
212 221
     for (int i = 0; i < fc; i++) {
213 222
         sl[i].tex = SDL_CreateTextureFromSurface(ren, isurf[i]);
214 223