1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include "../include/pongClass.h"
pongClass::pongClass()
{
pongWindow = NULL;
pongRenderer = NULL;
texLimitsTop = NULL;
texLimitsBot = NULL;
texPong = NULL;
surfPong = NULL;
surfLimitsTop = NULL;
surfLimitsBot = NULL;
rectLimitsTop.h = 12;
rectLimitsTop.w = width;
rectLimitsTop.x = 0;
rectLimitsTop.y = 0;
rectLimitsBot.h = 12;
rectLimitsBot.w = width;
rectLimitsBot.x = 0;
rectLimitsBot.y = height - rectLimitsBot.h;
rectPong.h = 120;
rectPong.w = 20;
rectPong.x = 17;
rectPong.y = height / 2 - rectPong.h / 2;
}
void pongClass::Clean()
{
SDL_DestroyTexture(texLimitsBot);
SDL_DestroyTexture(texLimitsTop);
SDL_DestroyTexture(texPong);
SDL_DestroyRenderer(pongRenderer);
SDL_DestroyWindow(pongWindow);
SDL_Quit();
}
bool pongClass::Init()
{
if(SDL_Init(SDL_INIT_VIDEO) != 0) return false;
pongWindow = SDL_CreateWindow("Pong", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width, height, SDL_WINDOW_SHOWN);
if(pongWindow == NULL) return false;
pongRenderer = SDL_CreateRenderer(pongWindow, -1,
SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
if(pongRenderer == NULL) return false;
SDL_SetRenderDrawColor(pongRenderer, 175, 175, 75, 255);
surfPong = IMG_Load("gfx\\pong.png");
surfLimitsTop = IMG_Load("gfx\\limits.png");
surfLimitsBot = IMG_Load("gfx\\limits.png");
if(surfPong == NULL||surfLimitsTop == NULL||surfLimitsBot == NULL) return false;
texPong = SDL_CreateTextureFromSurface(pongRenderer, surfPong);
texLimitsTop = SDL_CreateTextureFromSurface(pongRenderer, surfLimitsTop);
texLimitsBot = SDL_CreateTextureFromSurface(pongRenderer, surfLimitsBot);
if(texPong == NULL||texLimitsTop == NULL||texLimitsBot == NULL) return false;
SDL_FreeSurface(surfLimitsTop);
SDL_FreeSurface(surfLimitsBot);
SDL_FreeSurface(surfPong);
return true;
}
void pongClass::mainLoop()
{
timeLastUS = SDL_GetTicks();
updateScreen();
int a = 0;
Uint32 timeCheck = SDL_GetTicks();
while(!quit)
{
timeNow = SDL_GetTicks();
if(timeNow - timeLastUG >= aSec / lps)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)quit = true;
break;
case SDL_MOUSEMOTION:
updateGame(event.motion.y);
break;
default:
break;
}
}
timeNow = SDL_GetTicks();
if(timeNow - timeLastUS >= aSec / fps)
{
updateScreen();
}
timeLastUG = SDL_GetTicks();
}
}
}
void pongClass::updateGame(int X)
{
if(rectLimitsTop.y + rectLimitsTop.h >= X - rectPong.h /2)
{
rectPong.y = rectLimitsTop.y + rectLimitsTop.h;
}
else if(rectLimitsBot.y <= X + rectPong.h /2)
{
rectPong.y = rectLimitsBot.y - rectPong.h;
}
else
{
rectPong.y = X - rectPong.h / 2;
}
}
void pongClass::updateScreen()
{
SDL_RenderClear(pongRenderer);
SDL_RenderCopy(pongRenderer, texLimitsTop, NULL, &rectLimitsTop);
SDL_RenderCopy(pongRenderer, texLimitsBot, NULL, &rectLimitsBot);
SDL_RenderCopy(pongRenderer, texPong, NULL, &rectPong);
SDL_RenderPresent(pongRenderer);
}
| |