Advertisement
Guest User

Tiny SDL framebuffer thing for Starlight

a guest
Nov 3rd, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. // Tiny graphics sandbox for Starlight
  2.  
  3. #include <iostream>         // For cout
  4. #include <SDL.h>            // For SDL
  5.  
  6. using namespace std;        // So we don't have to type std::cout for cout
  7.  
  8. #define SCREEN_WIDTH 800    // How big do we want the window?
  9. #define SCREEN_HEIGHT 600
  10.  
  11. SDL_Surface *frameBuffer;   // This is our framebuffer memory!
  12.  
  13. // Set up all SDL stuffs
  14. void CreateSDLScreen() {
  15.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  16.         cout << "Unable to init SDL: " << SDL_GetError() << endl;
  17.         exit(1);
  18.     }
  19.  
  20.     frameBuffer = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  21.  
  22.     if (frameBuffer == NULL) {
  23.         cout << "Unable to set up video: " << SDL_GetError() << endl;
  24.         exit(1);
  25.     }
  26.     atexit(SDL_Quit);
  27. }
  28.  
  29. // Set a pixel
  30. void PutPixel(int x, int y, Uint32 color) {
  31.     if(SDL_MUSTLOCK(frameBuffer))
  32.         SDL_LockSurface(frameBuffer);
  33.     Uint8 *pixel = (Uint8 *)frameBuffer->pixels + (y * frameBuffer->pitch) + (x * sizeof(Uint32));
  34.     *((Uint32*)pixel) = color;
  35.     if(SDL_MUSTLOCK(frameBuffer))
  36.         SDL_UnlockSurface(frameBuffer);
  37. }
  38.  
  39. // Helper function: Get correct colour value from R, G, B
  40. Uint32 MakeColour(int r, int g, int b) {
  41.     return SDL_MapRGB(frameBuffer->format, r, g, b);
  42. }
  43.  
  44. // Helper function: Get a random number between 0 and some maximum
  45. int RandomNum(int max) {
  46.     return rand()%(max + 1);
  47. }
  48.  
  49. // Do a l33t effect \o/
  50. void DoEffect() {
  51.     // Pick some random coords
  52.     int x = RandomNum(SCREEN_WIDTH-1);
  53.     int y = RandomNum(SCREEN_HEIGHT-1);
  54.  
  55.     // Pick a random colour
  56.     int r = RandomNum(255);
  57.     int g = RandomNum(255);
  58.     int b = RandomNum(255);
  59.     int c = MakeColour(r, g, b);
  60.  
  61.     // Set a pixel!
  62.     PutPixel(x, y, c);
  63. }
  64.  
  65. // Proggy entrypoint
  66. int main (int argc, char** argv) {
  67.     // Let's bring up SDL!
  68.     CreateSDLScreen();
  69.  
  70.     // Flag so we know when to quit
  71.     bool done=false;
  72.  
  73.     // Repeat until done becomes true
  74.     while (!done) {
  75.         // Check to see if the window's been closed and quit if so
  76.         SDL_Event event;
  77.         while (SDL_PollEvent(&event)) {
  78.             if (event.type == SDL_QUIT)
  79.                 done=true;
  80.         }
  81.  
  82.         // Draw teh leet effect!
  83.         DoEffect();
  84.  
  85.         // Display the screen
  86.         SDL_Flip(frameBuffer);
  87.     }
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement