Skip to content

Instantly share code, notes, and snippets.

@mmozeiko
Last active March 3, 2024 10:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmozeiko/729860eeb414f1a2ee345d9d3ab4dd4e to your computer and use it in GitHub Desktop.
Save mmozeiko/729860eeb414f1a2ee345d9d3ab4dd4e to your computer and use it in GitHub Desktop.
drawing pixels with SDL
// on Windows compile with:
// cl.exe sdl2_pixels.c -Zi -Iinclude -link -incremental:no -subsystem:windows SDL2.lib SDL2main.lib shell32.lib
#include <SDL.h>
#include <math.h>
int main(int argc, char* argv[])
{
int width = 1280;
int height = 720;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("SDL pixels", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
SDL_Surface* screen = SDL_GetWindowSurface(window);
SDL_Surface* pixels = SDL_CreateRGBSurfaceWithFormat(0, width, height,32, SDL_PIXELFORMAT_RGBX8888);
unsigned int t1 = SDL_GetTicks();
float pos = 0;
for (;;)
{
SDL_Event ev;
while (SDL_PollEvent(&ev))
{
if (ev.type == SDL_QUIT)
{
return 0;
}
}
unsigned int t2 = SDL_GetTicks();
float delta = (t2 - t1) / 1000.0f;
t1 = t2;
// clear pixels to black background
SDL_FillRect(pixels, NULL, 0);
// write the pixels
SDL_LockSurface(pixels);
{
int pitch = pixels->pitch;
// move 100 pixels/second
pos += delta * 100.0f;
pos = fmodf(pos, width);
// draw red diagonal line
for (int i=0; i<height; i++)
{
int y = i;
int x = ((int)pos + i) % width;
unsigned int* row = (unsigned int*)((char*)pixels->pixels + pitch * y);
row[x] = 0xff0000ff; // 0xAABBGGRR
}
}
SDL_UnlockSurface(pixels);
// copy to window
SDL_BlitSurface(pixels, NULL, screen, NULL);
SDL_UpdateWindowSurface(window);
}
}
// on Windows compile with:
// cl.exe sdl2_pixels_r.c -Zi -Iinclude -link -incremental:no -subsystem:windows SDL2.lib SDL2main.lib shell32.lib
#include <SDL.h>
#include <math.h>
int main(int argc, char* argv[])
{
int width = 1280;
int height = 720;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("SDL pixels", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Texture* pixels = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STREAMING, width, height);
unsigned int t1 = SDL_GetTicks();
float pos = 0;
for (;;)
{
SDL_Event ev;
while (SDL_PollEvent(&ev))
{
if (ev.type == SDL_QUIT)
{
return 0;
}
}
unsigned int t2 = SDL_GetTicks();
float delta = (t2 - t1) / 1000.0f;
t1 = t2;
void* data;
int pitch;
SDL_LockTexture(pixels, NULL, &data, &pitch);
{
// clear to black background
SDL_memset(data, 0, pitch * height);
// move 100 pixels/second
pos += delta * 100.0f;
pos = fmodf(pos, width);
// draw red diagonal line
for (int i=0; i<height; i++)
{
int y = i;
int x = ((int)pos + i) % width;
unsigned int* row = (unsigned int*)((char*)data + pitch * y);
row[x] = 0xff0000ff; // 0xAABBGGRR
}
}
SDL_UnlockTexture(pixels);
// copy to window
SDL_RenderCopy(renderer, pixels, NULL, NULL);
SDL_RenderPresent(renderer);
}
}
@DeafMan1983
Copy link

DeafMan1983 commented Jan 26, 2024

Hello I found problem.
You should to remove {} after:
SDL_LockTexture(pixels, NULL, &data, &pitch);
It works fine. :)
Result:
image

Nice idea - I am working on C# :)

@mmozeiko
Copy link
Author

Curly braces like that are perfectly valid to use in C code. There's no reason to remove them. They just letting reader of source code to know that these lines of code should "go together" - because texture locking is happening around it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment