Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Tiny graphics sandbox for Starlight
- #include <iostream> // For cout
- #include <SDL.h> // For SDL
- using namespace std; // So we don't have to type std::cout for cout
- #define SCREEN_WIDTH 800 // How big do we want the window?
- #define SCREEN_HEIGHT 600
- SDL_Surface *frameBuffer; // This is our framebuffer memory!
- // Set up all SDL stuffs
- void CreateSDLScreen() {
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- cout << "Unable to init SDL: " << SDL_GetError() << endl;
- exit(1);
- }
- frameBuffer = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
- if (frameBuffer == NULL) {
- cout << "Unable to set up video: " << SDL_GetError() << endl;
- exit(1);
- }
- atexit(SDL_Quit);
- }
- // Set a pixel
- void PutPixel(int x, int y, Uint32 color) {
- if(SDL_MUSTLOCK(frameBuffer))
- SDL_LockSurface(frameBuffer);
- Uint8 *pixel = (Uint8 *)frameBuffer->pixels + (y * frameBuffer->pitch) + (x * sizeof(Uint32));
- *((Uint32*)pixel) = color;
- if(SDL_MUSTLOCK(frameBuffer))
- SDL_UnlockSurface(frameBuffer);
- }
- // Helper function: Get correct colour value from R, G, B
- Uint32 MakeColour(int r, int g, int b) {
- return SDL_MapRGB(frameBuffer->format, r, g, b);
- }
- // Helper function: Get a random number between 0 and some maximum
- int RandomNum(int max) {
- return rand()%(max + 1);
- }
- // Do a l33t effect \o/
- void DoEffect() {
- // Pick some random coords
- int x = RandomNum(SCREEN_WIDTH-1);
- int y = RandomNum(SCREEN_HEIGHT-1);
- // Pick a random colour
- int r = RandomNum(255);
- int g = RandomNum(255);
- int b = RandomNum(255);
- int c = MakeColour(r, g, b);
- // Set a pixel!
- PutPixel(x, y, c);
- }
- // Proggy entrypoint
- int main (int argc, char** argv) {
- // Let's bring up SDL!
- CreateSDLScreen();
- // Flag so we know when to quit
- bool done=false;
- // Repeat until done becomes true
- while (!done) {
- // Check to see if the window's been closed and quit if so
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- if (event.type == SDL_QUIT)
- done=true;
- }
- // Draw teh leet effect!
- DoEffect();
- // Display the screen
- SDL_Flip(frameBuffer);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement