#include <SDL2/SDL.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window *win = NULL;
SDL_Surface *scr = NULL;
SDL_Surface *mysurf = SDL_CreateRGBSurface(0,50,50,32,0,0,0,0);
int init() {
SDL_Init(SDL_INIT_VIDEO);
win = SDL_CreateWindow("mysurf", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
scr = SDL_GetWindowSurface(win);
return 0;
}
int quit() {
SDL_FreeSurface(mysurf);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
int random_255(){
return rand()%255;
}
int main (int argc, char ** args) {
srand(time (NULL));
init();
bool run = true;
SDL_Event e;
SDL_Rect r;
int x = 0;
int y = 0;
while (run) {
while(SDL_PollEvent(&e) != NULL) {
if (e.type == SDL_QUIT) {
run = false;
}
if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_UP) {
y -= 10;
}
if (e.key.keysym.sym == SDLK_DOWN) {
y += 10;
}
if (e.key.keysym.sym == SDLK_RIGHT) {
x += 10;
}
if (e.key.keysym.sym == SDLK_LEFT) {
x -= 10;
}
}
}
r.x = x;
r.y = y;
SDL_FillRect(scr, NULL, SDL_MapRGB(scr -> format, 255, 255, 255));
SDL_FillRect(mysurf,NULL,SDL_MapRGB(scr -> format, random_255(), random_255(), random_255()));
SDL_BlitSurface(mysurf, NULL, scr, &r);
SDL_UpdateWindowSurface(win);
}
return quit();
}
|