-
-
Save Logiraptor/11237431 to your computer and use it in GitHub Desktop.
#include <iostream> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_mixer.h> | |
#include <SDL2/SDL_image.h> | |
using namespace std; | |
SDL_Texture* LoadTexture(SDL_Renderer* render, const char* name); | |
bool processInput(bool b[4]); | |
int main (int argc, char* args[]) { | |
const int screen_width = 640; | |
const int screen_height =480; | |
bool b[4] = {0,0,0,0}; | |
SDL_Init(SDL_INIT_EVERYTHING); | |
SDL_Window* window = NULL; | |
window = SDL_CreateWindow("Pong", 100, 100, screen_width, screen_height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE ); | |
if (window == NULL) { | |
cout << "Window load Error!" << endl; | |
exit(1); | |
} | |
SDL_Renderer* render = NULL; | |
render = SDL_CreateRenderer(window, -1, 0); | |
if(render == NULL) { | |
cout << "Rendering Error!" << SDL_GetError() << endl; | |
exit(1); | |
} | |
SDL_Texture* stage1_image = NULL; | |
stage1_image = LoadTexture(render,"stage1.png"); | |
SDL_Rect stage1_rect; | |
stage1_rect.x = 10; | |
stage1_rect.y = 10; | |
stage1_rect.w = screen_width; | |
stage1_rect.h = screen_height; | |
SDL_Texture* red_bar_image = NULL; | |
red_bar_image = LoadTexture(render, "redbar.png"); | |
SDL_Rect red_bar_rect; | |
red_bar_rect.x = 10; | |
red_bar_rect.y = 240; | |
red_bar_rect.w = 50; | |
red_bar_rect.h = 50; | |
SDL_Texture* blue_bar_image = NULL; | |
blue_bar_image = LoadTexture(render, "bluebar.png"); | |
SDL_Rect blue_bar_rect; | |
blue_bar_rect.x = 580; | |
blue_bar_rect.y = 240; | |
blue_bar_rect.w = 50; | |
blue_bar_rect.h = 50; | |
SDL_Texture* ball_image = NULL; | |
ball_image = LoadTexture(render, "ball.png"); | |
SDL_Rect ball_rect; | |
ball_rect.x = 320; | |
ball_rect.y = 240; | |
ball_rect.w = 75; | |
ball_rect.h = 50; | |
//MainEvent is to poll event in wile loop | |
while(processInput(b)) { | |
if(b[0]) { | |
red_bar_rect.y -= 3; | |
} | |
if (b[1]) { | |
red_bar_rect.y += 3; | |
} | |
if(b[2]) { | |
blue_bar_rect.y -= 3; | |
} | |
if(b[3]) { | |
blue_bar_rect.y += 3; | |
} | |
ball_rect.x -= 5; | |
if (ball_rect.x <= red_bar_rect.x) { | |
ball_rect.x = 320; | |
} | |
SDL_RenderClear(render); | |
SDL_RenderCopy(render, stage1_image, NULL, &stage1_rect); | |
SDL_RenderCopy(render, red_bar_image, NULL, &red_bar_rect); | |
SDL_RenderCopy(render, blue_bar_image, NULL, &blue_bar_rect); | |
SDL_RenderCopy(render, ball_image, NULL, &ball_rect); | |
SDL_RenderPresent(render); | |
} | |
SDL_DestroyWindow(window); | |
SDL_DestroyRenderer(render); | |
SDL_DestroyTexture(stage1_image); | |
SDL_DestroyTexture(red_bar_image); | |
SDL_DestroyTexture(blue_bar_image); | |
SDL_DestroyTexture(ball_image); | |
} | |
SDL_Texture* LoadTexture(SDL_Renderer* render, const char* name) { | |
SDL_Texture* img = IMG_LoadTexture(render, name); | |
if (img == NULL) { | |
cout << "Error Loading Texture: " << SDL_GetError() << endl; | |
exit(1); | |
} | |
return img; | |
} | |
bool processInput(bool b[4]) { | |
static SDL_Event* mainevent = new SDL_Event(); | |
while (SDL_PollEvent(mainevent)) { | |
switch (mainevent->type) { | |
case SDL_QUIT: | |
return false; | |
break; | |
case SDL_KEYDOWN: | |
switch(mainevent->key.keysym.sym) { | |
case SDLK_w: | |
b[0] = 1; | |
break; | |
case SDLK_s: | |
b[1] = 1; | |
break; | |
case SDLK_o: | |
b[2] = 1; | |
break; | |
case SDLK_l: | |
b[3] = 1; | |
break; | |
case SDLK_ESCAPE: | |
return false; | |
break; | |
} | |
break; | |
case SDL_KEYUP: | |
switch(mainevent->key.keysym.sym) { | |
case SDLK_w: | |
b[0] = 0; | |
break; | |
case SDLK_s: | |
b[1] = 0; | |
break; | |
case SDLK_o: | |
b[2] = 0; | |
break; | |
case SDLK_l: | |
b[3] = 0; | |
} | |
break; | |
} | |
} | |
return true; | |
} |
So I made some more changes for readability. I moved the input logic into a function. This is important, because you should always try to separate pieces of code that aren't related as much as possible. Input is not related to the movement of a paddle, instead it is loosely coupled. It's like the difference between using a nail and using velcro. One of them allows you to change your mind later and make it work differently, the other makes it really hard to change anything without first breaking something. You'll notice that the controls are much more responsive now. That's because of the loose coupling.
Also, I noticed that in your initialization stages, you were checking for errors like this:
if(render == NULL)
{
cout << "Rendering Error!" << endl;
running = false;
}
It's great that you are checking for errors. That's extremely important and I have to fuss at my coworkers sometimes for not doing it, but it's also really important that you take the correct action when an error occurs. The way it was set up meant that, for example, if the renderer failed to initialize, then the program would still try to load a bunch of images... and then never use them. What you should do instead is what's called fail-fast. Basically you should try to take action as soon as possible after an error occurs. This makes it easier to fix the problem that caused the error. For instance, say this was an actual AAA game from Blizzard, and there was a problem with your video card and the Renderer can't load properly. If you don't fail fast, the user might have to wait 5 minutes staring at a black screen before the program finally quits and tells them something went wrong right when they clicked play. Ideally the program will immediately take action after an error. In a AAA game that probably means letting the user file a bug report with Blizzard. In this case, it's best to just quit the program and print an error message to the screen. In C++ you can quit the program from anywhere using the built-in function called exit
. You give it an int code for what happened. 0 means Everything's fine, 1 means something bad happened. So I've replaced those parts with this:
if(render == NULL) {
cout << "Rendering Error!" << SDL_GetError() << endl;
exit(1);
}
Awesome, i did not notice that. its has changed alot lol
SDL_Texture* LoadTexture(SDL_Renderer* render, const char* name);
bool processInput(bool b[4]);
what does that do ?
oh ok i see i had
bool b[4] = {0,0,0,0};
and you prototyped it.
sweet