Hello! I am new to SDL and am using it to write a custom graphical engine. I am running SDL2 2.0.10-1 on linux.
I’m (for now) trying to output some random noise to a window of your chosen size, but all that happens is a black window. I did get it to render something resembling random noise once when I was spamming the program to run. It looked more white than noise, kind of like it only had a couple of “blocks” of noise on a white background.
Run the program with ./main [width] [height]
main.c
#include <stdio.h>
#include <omp.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
if (argc != 3){
printf("Wrong amount of args given: expected width, height.\n");
return 1;
}
int w = atoi(argv[1]);
int h = atoi(argv[2]);
int pixels[w][h];
int pitch = w*4;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
SDL_Window *window;
SDL_Renderer *renderer;
SDL_CreateWindowAndRenderer(w, h, 0, &window, &renderer);
SDL_Texture* buffer = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
w,h);
SDL_LockTexture (buffer, NULL, (void **) &pixels, &pitch);
printf("\nStarting render...\n\nWidth: %d\nHeight: %d\n", w, h);
#pragma omp parallel for
for (int y = 0; y < h; ++y){
for (int x = 0; x < w; ++x){
pixels[x][y] = rand();
}
}
printf("parallel for ends.\n");
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, buffer, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_UnlockTexture(buffer);
SDL_Delay(2000);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
makefile
CC?=gcc
SDL2FLAGS=$(shell pkg-config sdl2 --cflags --libs)
CFLAGS?=-std=c11 -fopenmp -Wall -pedantic -Werror -Wshadow -Wstrict-aliasing -Wstrict-overflow
.PHONY: all msg clean fullclean
all: msg main
msg:
@echo --- C11 ---
main: main.c
${CC} ${CFLAGS} -O3 -o $@ $< ${SDL2FLAGS}
small: main.c
${CC} ${CFLAGS} -Os -o main $< ${SDL2FLAGS}
-strip main
-sstrip main
debug: main.c
${CC} ${CFLAGS} -O1 -g -o main $< ${SDL2FLAGS}
asm: main.asm
main.asm: main.c
${CC} ${CFLAGS} -S -o main.asm $< ${SDL2FLAGS}
run: msg main
time ./main
clean:
rm -f main *.o main.asm
fullclean: clean