So I'm starting SDL2 and ran through a few tutorials and decided that I would try to make a simple game and a clone of Pong seemed like the simplest. But I can't seem to be able to make the right hand side paddle draw on the window. At the moment I'm using SDL_FillRenderRect, one for each paddle but that just seems to make the left hand side one appear and not the right hand side. Here's my current code:
#include <iostream>
#include "Pong.h"
#include "Paddle.h"
//Screen size
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
bool quit = false;//Has user quit?
Paddle paddleLeft;
Paddle paddleRight;
int Pong::setup(){
//Initilize SDL
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("Pong",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (window == nullptr){
std::cout<<"SDL_CreateWindow error: "<<SDL_GetError()<<std::endl;
SDL_Quit();
return -1;
}
SDL_ShowCursor(0);
renderer = SDL_CreateRenderer(window,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr){
std::cout<<"SDL_CreateRenderer error: "<<SDL_GetError()<<std::endl;
SDL_Quit();
return -1;
}
int wH, wW;
//Setting the paddle to the centre of the left hand side of the screen.
SDL_GetWindowSize(window, &wW, &wH);
paddleLeft.setY((wH / 2) - (paddleLeft.getPaddleHeight() / 2));
paddleRight.setY((wH / 2) - (paddleLeft.getPaddleHeight() / 2));
//Setting the x, y, height and width of the left paddle
leftR.x = 20;
leftR.y = paddleLeft.getY();
leftR.h = paddleLeft.getPaddleWidth();
leftR.w = paddleLeft.getPaddleHeight();
//Setting the x, y, height and width of the right paddle
rightR.x = 620;
rightR.y = paddleLeft.getY();
rightR.h = paddleLeft.getPaddleWidth();
rightR.w = paddleLeft.getPaddleHeight();
return 1;//If setup fails then return -1 otherwise return 1
}
void Pong::updateGame(){
SDL_Event event;
while (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT){
quit = true;
}
if (event.type == SDL_KEYDOWN){
switch (event.key.keysym.sym){
case SDLK_w: paddleLeft.setY(paddleLeft.getY() - paddleLeft.getSpeed()); break;
case SDLK_s: paddleLeft.setY(paddleLeft.getY() + paddleLeft.getSpeed()); break;
}
}
}
}
void Pong::render(){
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
leftR.x = paddleLeft.getX();
leftR.y = paddleLeft.getY();
rightR.x = paddleRight.getX();
rightR.y = paddleRight.getY();
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &leftR);
SDL_RenderFillRect(renderer, &rightR);
SDL_RenderPresent(renderer);
}
void Pong::cleanup(){
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void Pong::run(){
while (!quit){
updateGame();
render();
}
cleanup();
}
Could someone please point me in the correct direction for drawing the right paddle? I would also appreciate it if there are anyother errors in my code or ways to make it more efficent and what not if that could also be pointed out. Thanks in advance.
Read this closely:
rightR.x = 620;
rightR.y = paddleLeft.getY();
rightR.h = paddleLeft.getPaddleWidth();
rightR.w = paddleLeft.getPaddleHeight();
right and left mixed up due to copy-paste programming. I'm not saying it's bad, I do it too to reduce harm on my hands. You should just check things like this twice.
Also, in your case, you should start using OOP concepts. Don't perform everything in non-member functions, make member functions that perform on instances instead. I don't know where the values of getY
, getPaddleWidth
and getPaddleHeight
come from, but it's clear to me that there shouldn't be anything other than SDL_Rect
holding them.
I even think that SDL is a waste of time and I can't remember what's C++ about it. It's pure C and you're not going to learn anything useful. SFML is far better choice.