Skip to content

Instantly share code, notes, and snippets.

@Logiraptor
Forked from anonymous/pong1.txt
Last active August 29, 2015 14:00
Show Gist options
  • Save Logiraptor/11237431 to your computer and use it in GitHub Desktop.
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;
}
@warlok900
Copy link

oh ok i see i had
bool b[4] = {0,0,0,0};
and you prototyped it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment