Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include <stdio.h>
- #include <SDL.h>
- #define WIDTH 640
- #define HEIGHT 480
- #define BPP 4
- #define DEPTH 32
- void SetPixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
- {
- int ytimesw = y*screen->pitch/BPP;
- Uint32 *pixmem32 = (Uint32*) screen->pixels + ytimesw + x;
- *pixmem32 = SDL_MapRGB( screen->format, r, g, b );
- }
- void ClearSurface(SDL_Surface* surf, Uint8 r, Uint8 g, Uint8 b)
- {
- int w = surf->w;
- int h = surf->h;
- for(int y=0 ; y < h ; y++)
- {
- for(int x=0 ; x < w ; x++)
- {
- SetPixel(surf, x, y, r, g ,b);
- }
- }
- }
- void DrawScreen(SDL_Surface* screen)
- {
- for(int y=0 ; y < HEIGHT ; y++)
- {
- for(int x=0 ; x < WIDTH ; x++)
- {
- float fr = 255.0f * float(x) / float(WIDTH-1);
- float fg = 255.0f * float(y) / float(HEIGHT-1);
- SetPixel(screen, x, y, (Uint8)fr, (Uint8)fg, 0x00);
- }
- }
- }
- int main(int argc, char* argv[])
- {
- SDL_Surface *screen;
- SDL_Event evt;
- int keypress = 0;
- int h=0;
- if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
- if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE)))
- {
- SDL_Quit();
- return 1;
- }
- while(!keypress)
- {
- bool bSkip = false;
- if(SDL_MUSTLOCK(screen))
- if(SDL_LockSurface(screen) < 0)
- bSkip = true;
- if(!bSkip)
- {
- DrawScreen(screen);
- if(SDL_MUSTLOCK(screen))
- SDL_UnlockSurface(screen);
- SDL_Flip(screen);
- }
- while(SDL_PollEvent(&evt))
- {
- switch (evt.type)
- {
- case SDL_QUIT:
- keypress = 1;
- break;
- //case SDL_KEYDOWN:
- // keypress = 1;
- // break;
- default:
- break;
- }
- }
- }
- SDL_Quit();
- return 0;
- }
Add Comment
Please, Sign In to add comment