Skip to content

Instantly share code, notes, and snippets.

@wblakecaldwell
Created May 4, 2023 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wblakecaldwell/782c19388037f9f105b3c41cff08e9b2 to your computer and use it in GitHub Desktop.
Save wblakecaldwell/782c19388037f9f105b3c41cff08e9b2 to your computer and use it in GitHub Desktop.
Chat GPT4 writing C++ SDL2 code to fill a screen, giving each pixel a random color, regenerating every 1/24th of a second
#include <SDL2/SDL.h>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
int main(int argc, char *argv[]) {
srand(static_cast<unsigned>(time(0)));
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Error initializing SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window *window = SDL_CreateWindow("Random Pixels",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (!window) {
printf("Error creating window: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
printf("Error creating renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Event event;
bool running = true;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
// Clear the screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Draw random-colored pixels
for (int x = 0; x < WINDOW_WIDTH; ++x) {
for (int y = 0; y < WINDOW_HEIGHT; ++y) {
Uint8 r = rand() % 256;
Uint8 g = rand() % 256;
Uint8 b = rand() % 256;
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_RenderDrawPoint(renderer, x, y);
}
}
// Update the screen
SDL_RenderPresent(renderer);
SDL_Delay(1000 / 24); // Delay for 1/24th of a second
}
// Clean up
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment