diff options
author | Gentoo <installgentoo@endianness.com> | 2021-03-27 10:05:55 +1100 |
---|---|---|
committer | Gentoo <installgentoo@endianness.com> | 2021-03-27 10:05:55 +1100 |
commit | 4a09b7bc0b8229495cd1807cc1bb9bf5f485c2ed (patch) | |
tree | c6002baa7973e724c665e942c92cf1eba0edac55 /main.c | |
download | sdl2-checkerboard-4a09b7bc0b8229495cd1807cc1bb9bf5f485c2ed.tar.gz sdl2-checkerboard-4a09b7bc0b8229495cd1807cc1bb9bf5f485c2ed.tar.bz2 sdl2-checkerboard-4a09b7bc0b8229495cd1807cc1bb9bf5f485c2ed.zip |
initial commit
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 80 |
1 files changed, 80 insertions, 0 deletions
@@ -0,0 +1,80 @@ +#include <SDL2/SDL.h> +#include <stdio.h> +#include <stdlib.h> + +#include "lookup.h"//contains a 2D array mapped to the display + +#define WIDTH 64 +#define HEIGHT 32 + +int main(int argc, char *argv[]) +{ + SDL_Window *window; + SDL_Renderer *renderer; + SDL_Texture *texture; + SDL_Event event; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); + return 3; + } + + window = SDL_CreateWindow("SDL_CreateTexture", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + WIDTH*16, HEIGHT*16, + SDL_WINDOW_RESIZABLE); + + renderer = SDL_CreateRenderer(window, -1, 0); + + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);//create a texture for direct pixel access + + int pitch;//pixel pitch + int *pixels = NULL;//pointer that will later point to the pixels + + + while (1) + { + int ret = SDL_LockTexture(texture, NULL, (void **) &pixels, &pitch);//lock the texture for direct pixel access + if (ret != 0){SDL_Log("ret=%d pix=%p error=%s", ret, pixels, SDL_GetError());} + + //display checkerboard pattern + int i, j; + for (i=0; i<32; ++i) + { + if (i % 2 == 0) + { + for (j=0; j<64; j+=2) + { + pixels[display[i][j]] = 0xFFFFFFFF; //white + } + } + else + { + for (j=1; j<64; j+=2) + { + pixels[display[i][j]] = 0xFFFFFFFF; //white + } + } + } + +// pixels[2047] = 0xfff0;//blue + +// pixels = pixels + pitch; //pitch doesn't need to be changed + + SDL_UnlockTexture(texture); + + SDL_RenderCopy(renderer, texture, NULL, NULL);//copy the texture to the rendered + SDL_RenderPresent(renderer);//render the texture to the renderer + + //SDL delay seems to case a bug when pressing the x or Alt+F4 does not close for some time + SDL_Delay(80);//An ~80ms delay between redrawing and polling for SDL_QUIT saves CPU time while still retaining quick resizing and closing + SDL_PollEvent(&event); + if(event.type == SDL_QUIT){break;} + } + + SDL_DestroyRenderer(renderer); + SDL_Quit(); + return 0; +} |