#include <iostream>
#include "objects.h"
#include "SDL/SDL.h"
#include "SDL/SDL_draw.h"
SDL_Surface* window = NULL;
SDL_Event event;
bool quit = false;
ball one;
paddle player;
int update(int background_color, int paddle_color, SDL_Rect background)
{
if (SDL_FillRect(window,&background,background_color) == -1)
{
return 1;
}
Draw_FillCircle(window,one.x,one.y,10,paddle_color);
player.pad.y += player.velocity;
if (player.pad.y < 0)
{
player.pad.y = 0;
}
if (player.pad.y > 400)
{
player.pad.y = 400;
}
if (SDL_FillRect(window,&player.pad,paddle_color) == -1)
{
return 1;
}
if (SDL_Flip(window) == -1)
{
return 1;
}
return 0;
}
int main()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return 1;
}
window = SDL_SetVideoMode(620, 470, 32, SDL_SWSURFACE);
if (window == NULL)
{
return 1;
}
int background_color = SDL_MapRGB(window->format,23,54,200);
SDL_Rect background;
background.x = 0;
background.y = 0;
background.w = 620;
background.h = 470;
int paddle_color = SDL_MapRGB(window->format, 235,4,55);
SDL_WM_SetCaption("Ping",NULL);
int begin = 0;
while (quit == false)
{
begin = SDL_GetTicks();
while(SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
player.velocity -= 5;
break;
case SDLK_DOWN:
player.velocity += 5;
break;
default:
break;
}
}
if (event.type == SDL_KEYUP)
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
player.velocity += 5;
break;
case SDLK_DOWN:
player.velocity -= 5;
break;
default:
break;
}
}
if (event.type == SDL_QUIT)
{
quit = true;
}
}
if(update(background_color, paddle_color, background) == 1)
{
return 0;
}
if ((SDL_GetTicks() - begin) < 20)
{
SDL_Delay(20 - SDL_GetTicks() + begin);
}
}
SDL_Quit();
return 0;
}