#include <SDL2/SDL.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
const int SCREEN_WIDTH = 500;
const int SCREEN_HEIGHT = 500;
SDL_Window *win = NULL;
SDL_Surface *scr = NULL;
SDL_Renderer* gRenderer = NULL;
SDL_Rect fillRect = { 0, 0,49,49 };
int init() {
SDL_Init(SDL_INIT_VIDEO);
win = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
scr = SDL_GetWindowSurface(win);
gRenderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED );
SetWindowText(GetActiveWindow(),"Как дела с WinApi?");
return 0;
}
int quit() {
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
void draw_table()
{
SDL_Rect fillR = { 0, 0, 500, 500 };
SDL_SetRenderDrawColor( gRenderer, 0, 0, 0, 0);
SDL_RenderFillRect( gRenderer, &fillR );
SDL_SetRenderDrawColor( gRenderer, 255,255 , 255, 0 );
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
fillRect.x = i*50;
fillRect.y = j*50;
SDL_RenderFillRect( gRenderer, &fillRect );
}
}
}
int main (int argc, char ** args) {
srand(time (NULL));
init();
bool run = true;
SDL_Event e;
int x = 0;
int y = 0;
SDL_Rect playRect = { 0, 0,49,49 };
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 -= 50;
}
if (e.key.keysym.sym == SDLK_DOWN) {
y += 50;
}
if (e.key.keysym.sym == SDLK_RIGHT) {
x += 50;
}
if (e.key.keysym.sym == SDLK_LEFT) {
x -= 50;
}
}
}
if (x < 0) x = 0;
if (x > 450) x = 450;
if (y < 0) y = 0;
if (y > 450) y = 450;
playRect.x = x;
playRect.y = y;
draw_table();
SDL_SetRenderDrawColor( gRenderer, 0x00, 0xFF, 0x00, 0xFF );
SDL_RenderFillRect( gRenderer, &playRect );
SDL_RenderPresent( gRenderer );
}
return quit();
}
|