Skip to content

Instantly share code, notes, and snippets.

Created April 24, 2014 00:06
Show Gist options
  • Save anonymous/11236868 to your computer and use it in GitHub Desktop.
Save anonymous/11236868 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <SDL.h>
#include <SDL_mixer.h>
#include <SDL_image.h>
using namespace std;
int main (int argc, char* args[])
{
const int screen_width = 640;
const int screen_height =480;
const int FPS = 200;
bool b[4] = {0,0,0,0,};
bool running = true;
Uint32 start;
SDL_Event* ball;
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;
running = false;
}
SDL_Renderer* render = NULL;
render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);
if(render == NULL)
{
cout << "Rendering Error!" << endl;
running = false;
}
SDL_Texture* stage1_image = NULL;
stage1_image = IMG_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 = IMG_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 = IMG_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 = IMG_LoadTexture(render, "ball.png");
SDL_Rect ball_rect;
ball_rect.x = 320;
ball_rect.y = 240;
ball_rect.w = 75;
ball_rect.h = 50;
SDL_Event* mainevent = new SDL_Event();
//MainEvent is to poll event in wile loop
while(running)
{
start = SDL_GetTicks();
while (SDL_PollEvent(mainevent))
{
switch (mainevent->type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
ball_rect.x --;
ball_rect.x --;
ball_rect.x --;
ball_rect.x --;
ball_rect.x --;
ball_rect.x --;
ball_rect.x --;
ball_rect.x --;
ball_rect.x --;
ball_rect.x --;
ball_rect.x --;
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:
running =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;
}
if(b[0])
{
red_bar_rect.y--;
red_bar_rect.y--;
red_bar_rect.y--;
}
if (b[1])
{
red_bar_rect.y++;
red_bar_rect.y++;
red_bar_rect.y++;
}
if(b[2])
{
blue_bar_rect.y--;
blue_bar_rect.y--;
blue_bar_rect.y--;
}
if(b[3])
{
blue_bar_rect.y++;
blue_bar_rect.y++;
blue_bar_rect.y++;
}
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);
}
ball_rect.x--;
ball_rect.x--;
ball_rect.x--;
ball_rect.x--;
ball_rect.x--;
if (ball_rect.x <= red_bar_rect.x)
{
ball_rect.x = 320;
}
/*if(1000/FPS > SDL_GetTicks() - start)
{
SDL_Delay(1000/FPS - (SDL_GetTicks() - start));
}*/
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);
}
@warlok900
Copy link

bool collision(SDL_Rect* rect1, SDL_Rect* rect2)
{
    if (rect1->y >= rect2->y + rect2->h)
    {
        return 0;
        if (rect1->x >= rect2->x + rect2->w)
        {
            return 0;
            if (rect1->y + rect1->h <= rect2->y)
            {
                return 0;
                if (rect1->x + rect1->w <= rect2->x)
                {
                    return 0;
                }
            }
        }
    }
    return 1;
}

so i do not see anything wrong with this. but my compiler says: a function definition is not allowed here before "{" token.

@Logiraptor
Copy link

That looks correct to me. It's probably related to where you tried to define it. Also, I've updated my fork with some more improvements and long explanations about why I did them that way.

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