Last active
August 27, 2018 13:39
-
-
Save krisimmig/20089d5bca3ec0aa0c489b24c81e4595 to your computer and use it in GitHub Desktop.
SDL and pixels.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <SDL2/SDL.h> | |
#include "../include/fastnoise/FastNoise.h" | |
// Screen dimension constants | |
const int SCREEN_WIDTH = 1000; | |
const int SCREEN_HEIGHT = 1000; | |
// Starts up SDL and creates window | |
bool init(); | |
// Frees media and shuts down SDL | |
void close(); | |
// Current displayed image | |
SDL_Surface *gCurrentSurface = NULL; | |
// The window we'll be rendering to | |
SDL_Window *gWindow = NULL; | |
// The window renderer | |
SDL_Renderer *gRenderer = NULL; | |
SDL_Texture *texture = NULL; | |
Uint32 *pixels = NULL; | |
bool init() | |
{ | |
// Initialization flag | |
SDL_Init(SDL_INIT_VIDEO); | |
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); | |
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); | |
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); | |
texture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 980, 980); | |
pixels = new Uint32[980 * 980]; | |
return true; | |
} | |
int main(int argc, char *args[]) | |
{ | |
// Start up SDL and create window | |
if (!init()) | |
{ | |
printf("Failed to initialize!\n"); | |
} | |
else | |
{ | |
// Main loop flag | |
bool quit = false; | |
// Event handler | |
SDL_Event e; | |
// While application is running | |
while (!quit) | |
{ | |
// Handle events on queue | |
while (SDL_PollEvent(&e) != 0) | |
{ | |
// User requests quit | |
if (e.type == SDL_QUIT) | |
{ | |
quit = true; | |
} | |
} | |
Uint32 format; | |
int w, h; | |
int pitch = 0; | |
SDL_QueryTexture(texture, &format, nullptr, &w, &h); | |
SDL_PixelFormat pixelFormat; | |
pixelFormat.format = format; | |
Uint32 color = SDL_MapRGB(&pixelFormat, 10, 200, 10); | |
SDL_LockTexture(texture, NULL, (void **)&pixels, &pitch); | |
// set pixels to solid white | |
for (int i = 0; i < pitch * 980; i++) | |
{ | |
pixels[i] = color; | |
} | |
SDL_UnlockTexture(texture); | |
SDL_Rect dst; | |
dst.x = 10; | |
dst.y = 10; | |
dst.w = 980; | |
dst.h = 980; | |
SDL_RenderClear(gRenderer); | |
SDL_RenderCopy(gRenderer, texture, NULL, &dst); | |
// Update screen | |
SDL_RenderPresent(gRenderer); | |
} | |
} | |
// Free resources and close SDL | |
close(); | |
return 0; | |
} | |
void close() | |
{ | |
// Destroy window | |
SDL_DestroyWindow(gWindow); | |
SDL_DestroyRenderer(gRenderer); | |
gWindow = NULL; | |
gRenderer = NULL; | |
// Quit SDL subsystems | |
SDL_Quit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment