/* Copyright (C) 2021 Gentoo-libre Install
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see .
*/
#include
#include
#include
#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;
}