1
0
Fork 0

Define constants, set pad move limits

This commit is contained in:
Gregory Eremin 2015-09-30 14:56:05 +03:00
parent b7b1780f9e
commit 1ae4028603
No known key found for this signature in database
GPG Key ID: 5EFA427EEC26E86C
1 changed files with 33 additions and 15 deletions

View File

@ -1,6 +1,18 @@
#include <iostream> #include <iostream>
#include "SDL2/SDL.h" #include "SDL2/SDL.h"
#define SCREEN_X 100
#define SCREEN_Y 100
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define PAD_WIDTH 200
#define PAD_HEIGHT 20
#define KEY_MOVE_STEP 5
#define BG_COLOR 0, 0, 0, 255
#define PAD_COLOR 0, 0, 255, 255
int main(int argc, char const *argv[]) { int main(int argc, char const *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) { if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
@ -8,10 +20,10 @@ int main(int argc, char const *argv[]) {
} }
SDL_Window *window = SDL_CreateWindow( SDL_Window *window = SDL_CreateWindow(
"Breakout", // Title "Breakout",
100, 100, // X, Y SCREEN_X, SCREEN_Y,
640, 480, // Width, Height SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN // Visibility SDL_WINDOW_SHOWN
); );
if (window == nullptr) { if (window == nullptr) {
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
@ -32,19 +44,19 @@ int main(int argc, char const *argv[]) {
} }
// Setting background color to black // Setting background color to black
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, BG_COLOR);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
// Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high. // Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high.
SDL_Rect platform = { SDL_Rect platform = {
.x = (640 - 200) / 2, .x = (SCREEN_WIDTH - PAD_WIDTH) / 2,
.y = (480 - 20), .y = (SCREEN_HEIGHT - PAD_HEIGHT),
.w = 200, .w = PAD_WIDTH,
.h = 20 .h = PAD_HEIGHT
}; };
// Set render color to blue ( rect will be rendered in this color ) // Set render color to blue ( rect will be rendered in this color )
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); SDL_SetRenderDrawColor(renderer, PAD_COLOR);
// Render rect // Render rect
SDL_RenderFillRect(renderer, &platform); SDL_RenderFillRect(renderer, &platform);
@ -65,22 +77,28 @@ int main(int argc, char const *argv[]) {
quit = true; quit = true;
break; break;
case SDLK_LEFT: case SDLK_LEFT:
platform.x-=5; platform.x -= KEY_MOVE_STEP;
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
platform.x+=5; platform.x += KEY_MOVE_STEP;
break; break;
} }
} }
if (e.type == SDL_MOUSEMOTION) { if (e.type == SDL_MOUSEMOTION) {
platform.x = e.motion.x - 100; platform.x = e.motion.x - PAD_WIDTH / 2;
}
if (platform.x < 0) {
platform.x = 0;
} else if (platform.x > SCREEN_WIDTH - PAD_WIDTH) {
platform.x = SCREEN_WIDTH - PAD_WIDTH;
} }
} }
// Re-rendering // Re-rendering
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, BG_COLOR);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); SDL_SetRenderDrawColor(renderer, PAD_COLOR);
SDL_RenderFillRect(renderer, &platform); SDL_RenderFillRect(renderer, &platform);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
SDL_Delay(1); SDL_Delay(1);