How to draw a custom shaped element? - c++

I'm currently working in an app which is essentially a pong game. The game features two paddles currently in rectangular format to ease collision detection against the ball(currently square).
I'm using a "QRect" element as paddle since it provides the ".intersect" method, making it easy to check for collisions.
My implementation for a rectangular paddle is as follows:
Paddle::Paddle(int initial_x, int initial_y) {
QImage image.load(":images/paddle.png");
QRect rect = image.rect();
resetState(initial_x, initial_y);
}
I'm trying to draw an arch like paddle and hitbox, similar to what the code below provides:
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
QPainter painter(this);
painter.drawChord(rectangle, startAngle, spanAngle);
The only problem with the code above is it only works inside a paintEvent function and that won't work for me.
paddle.h
#ifndef PADDLE_H
#pragma once
#include <QImage>
#include <QRect>
class Paddle {
public:
Paddle(int, int);
~Paddle();
public:
void resetState(int, int);
void move();
void setDx(int);
void setDy(int);
QRect getRect();
QImage & getImage();
private:
QImage image;
QRect rect;
int dx;
int dy;
static const int INITIAL_X1 = 70;
static const int INITIAL_Y1 = 350;
};
#define PADDLE_H
#endif // PADDLE_H
paddle.cpp
#include <iostream>
#include "paddle.h"
Paddle::Paddle(int initial_x, int initial_y) {
dy = 0;
image.load(":images/paddle.png");
rect = image.rect();
resetState(initial_x, initial_y);
}
Paddle::~Paddle() {
std::cout << ("Paddle deleted") << std::endl;
}
void Paddle::setDy(int y) {
dy = y;
}
void Paddle::move() {
int x = rect.x();
int y = rect.top() + dy;
rect.moveTo(x, y);
}
void Paddle::resetState(int initial_x, int initial_y) {
rect.moveTo(initial_x, initial_y);
}
QRect Paddle::getRect() {
return rect;
}
QImage & Paddle::getImage() {
return image;
}
mainGame.h
#ifndef BREAKOUT_H
#pragma once
#include <QWidget>
#include <QKeyEvent>
#include <QFrame>
#include "ball.h"
#include "brick.h"
#include "paddle.h"
class Breakout : public QFrame {
Q_OBJECT
public:
Breakout(QWidget *parent = 0);
~Breakout();
signals:
void leftScoreChanged(int leftScore);
void rightScoreChanged(int rightScore);
void ballLost(int ballsLeft);
protected:
void paintEvent(QPaintEvent *);
void timerEvent(QTimerEvent *);
void keyPressEvent(QKeyEvent *);
void keyReleaseEvent(QKeyEvent *);
void drawObjects(QPainter *);
void finishGame(QPainter *, QString);
void moveObjects();
void startGame();
void pauseGame();
void stopGame();
void victory();
void validateScoreChange(int);
void checkCollision();
private:
int x;
int timerId;
int ballsLeft;
int leftScore;
int rightScore;
static const int N_OF_BRICKS = 30;
static const int DELAY = 5;
static const int TOP_EDGE = 0;
static const int LEFT_EDGE = 0;
static const int BOTTOM_EDGE = 700;
static const int RIGHT_EDGE = 1200;
Ball *ball;
Paddle *leftPaddle;
Paddle *rightPaddle;
Brick *bricks[N_OF_BRICKS];
bool gameOver;
bool gameWon;
bool gameStarted;
bool paused;
};
#define BREAKOUT_H
#endif // BREAKOUT_H
Breakout::paintEvent()
void Breakout::paintEvent(QPaintEvent *e) {
Q_UNUSED(e);
QPainter painter(this);
if (gameOver) {
finishGame(&painter, "Game lost");
} else if(gameWon) {
finishGame(&painter, "Victory");
}
else {
drawObjects(&painter);
}
QWidget::paintEvent(e);
}

From what I understand you do not want to load the QImage from a .png file but you want to draw it, if so you can use QPainter to create the image as I show below:
Paddle::Paddle(int initial_x, int initial_y) {
// draw image
QRectF rectangle(10.0, 20.0, 80.0, 60.0);
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
image = QImage(QSize(100, 100), QImage::Format_ARGB32);
image.fill(Qt::transparent);
QPainter painter(&image);
painter.drawChord(rectangle, startAngle, spanAngle);
painter.end();
dy = 0;
rect = image.rect();
resetState(initial_x, initial_y);
}

Related

How to create a layout of bricks for Breakout game using a data/text file in C++ SFML?

I am creating a classic breakout game in C++ using SFML library. So far I have a movable paddle and ball implemented.
Currently, my goal is to create a layout of bricks. So far I have it so that a single brick is displayed. However, I want to make it so that I can draw a layout of bricks in text file using a letter (For example using letter 'B'), read that text file's brick layout and draw that layout in game.
I am not sure how to do this so any help would be great!
Here is my code so far (Note that I did not include for Paddle and Ball as I thought it was not needed for this problem):
GameObject.h
#pragma once
#include <SFML/Graphics.hpp>
class GameObject
{
protected:
sf::Vector2f position;
float speed;
sf::RenderWindow& m_window;
public:
GameObject(float startX, float startY, sf::RenderWindow& window);
virtual ~GameObject() {};
virtual void Draw() = 0;
virtual void Update() = 0;
};
GameObject.cpp
#include "GameObject.h"
GameObject::GameObject(float startX, float startY, sf::RenderWindow& window)
: position{ startX, startY }
, speed{ 0.5f }
, m_window{ window }
{
}
Brick.h
#pragma once
#include "GameObject.h"
class Brick : public GameObject
{
private:
sf::RectangleShape brickShape;
static constexpr int shapeWidth = 50;
static constexpr int shapeHeight = 20;
int color;
int strength;
public:
Brick(float startX, float startY, sf::RenderWindow& window);
sf::FloatRect getPosition();
sf::RectangleShape getShape();
int getStrength();
void setStrength(int strengthValue);
void Draw() override;
void Update() override;
};
Brick.cpp
#include "Brick.h"
Brick::Brick(float startX, float startY, sf::RenderWindow& window)
: GameObject{ startX, startY, window }
{
brickShape.setSize(sf::Vector2f(shapeWidth, shapeHeight));
brickShape.setPosition(position);
color = (rand() % 2) + 1;
if (color == 1)
{
brickShape.setFillColor(sf::Color::Yellow);
strength = 1;
}
else
{
brickShape.setFillColor(sf::Color(255, 165, 0));
strength = 2;
}
}
sf::FloatRect Brick::getPosition()
{
return brickShape.getGlobalBounds();
}
sf::RectangleShape Brick::getShape()
{
return brickShape;
}
int Brick::getStrength()
{
return strength;
}
void Brick::setStrength(int strengthValue)
{
strength = strengthValue;
}
void Brick::Draw()
{
m_window.draw(brickShape);
}
void Brick::Update()
{
}
Game.h
#pragma once
#include <SFML/Graphics.hpp>
#include "Paddle.h"
#include "Ball.h"
#include "Brick.h"
#include <algorithm>
#include <fstream>
#include <iostream>
class Game
{
private:
sf::RenderWindow& m_window;
std::unique_ptr<Paddle> player;
std::unique_ptr<Ball> ball;
std::unique_ptr<Brick> brick;
std::vector<std::unique_ptr<Brick>>bricks;
int bricksCountX;
int bricksCountY;
public:
const unsigned int m_windowWidth;
const unsigned int m_windowHeight;
public:
Game(sf::RenderWindow& window, const unsigned int& windowWidth, const unsigned int& windowHeight);
float RandomFloat(float a, float b);
void ReadFile();
void HandleCollision();
void HandleInput();
void Draw();
void Update();
void Run();
};
Game.cpp
#include "Game.h"
Game::Game(sf::RenderWindow& window, const unsigned int& windowWidth, const unsigned int& windowHeight)
: m_window{ window }
, m_windowWidth{ windowWidth }
, m_windowHeight{ windowHeight }
{
player = std::make_unique<Paddle>(m_windowWidth/2, m_windowHeight - 70, m_window);
ball = std::make_unique<Ball>(m_windowWidth / 2, m_windowHeight / 2, m_window);
ReadFile();
for (int i = 0; i < bricksCountX; i++)
for (int j = 0; j < bricksCountY; j++)
bricks.emplace_back(std::make_unique<Brick>((i + 1.5) * ((long long)brick->getShape().getSize().x + 3) + 22,
(j + 5) * (brick->getShape().getSize().y + 3), m_window));
}
float Game::RandomFloat(float a, float b)
{
return ((b - a) * ((float)rand() / RAND_MAX)) + a;
}
void Game::ReadFile()
{
// Create a text string, which is used to output the text file
std::string bricksText;
int numOfLines = 0;
// Read from the text file
std::ifstream MyReadFile("Bricks Layout.txt");
// Use a while loop together with the getline() function to read the file line by line
while (std::getline(MyReadFile, bricksText))
{
++numOfLines;
// Output the text from the file
bricksCountX = bricksText.length();
std::cout << bricksCountX;
}
bricksCountY = numOfLines;
// Close the file
MyReadFile.close();
}
void Game::HandleCollision()
{
if (ball->getShape().getPosition().x - ball->getRadius() < 0.0f)
{
ball->reboundLeft();
}
else if (ball->getShape().getPosition().x + ball->getRadius() > m_windowWidth)
{
ball->reboundRight();
}
else if (ball->getShape().getPosition().y - ball->getRadius() < 0.0f)
{
ball->reboundTop();
}
else if (ball->getShape().getPosition().y + ball->getRadius() > m_windowHeight)
{
ball->ballAngle = ball->ballAngle * -1;
}
else if (ball->getPosition().intersects(player->getPosition()))
{
ball->reboundPaddle(*player);
}
for (unsigned int i = 0; i < bricks.size(); i++)
{
if (ball->getPosition().intersects(bricks[i]->getPosition()))
{
if (bricks[i]->getStrength() == 1)
{
ball->reboundBrick(*bricks[i]);
bricks.erase(std::remove(bricks.begin(), bricks.end(), bricks[i]), bricks.end());
}
else
{
ball->reboundBrick(*bricks[i]);
bricks[i]->setStrength(1);
}
}
}
}
void Game::HandleInput()
{
player->HandleInput();
}
void Game::Draw()
{
player->Draw();
ball->Draw();
for (unsigned int i = 0; i < bricks.size(); i++)
{
bricks[i]->Draw();
}
}
void Game::Update()
{
player->Update();
ball->Update();
brick->Update();
}
void Game::Run()
{
//Game Loop
while (m_window.isOpen())
{
sf::Event event;
while (m_window.pollEvent(event))
{
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
m_window.close();
}
m_window.clear();
Draw();
HandleInput();
HandleCollision();
Update();
m_window.display();
}
}

No type named 'VideoCapture' in namespace 'cv'

hi i'm a beginner trying to learn openFrameworks and I get this error when I run my code. I've been looking around and cannot find a solution, please help. It is running on xcode and I have managed to run previous apps before.
ofApp.h
#pragma once
#include "ofMain.h"
#include "opencv2/opencv.hpp"
class ofApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void keyPressed(int key) {};
void keyReleased(int key) {};
void mouseMoved(int x, int y) {};
void mouseDragged(int x, int y, int button) {};
void mousePressed(int x, int y, int button) {};
void mouseReleased(int x, int y, int button) {};
void windowResized(int w, int h) {};
void dragEvent(ofDragInfo dragInfo) {};
void gotMessage(ofMessage msg) {};
ofEasyCam cam;
cv::VideoCapture cap;
cv::Size cap_size;
int number_of_frames;
vector<cv::Mat> frame_list;
ofImage image;
cv::Mat frame;
vector<ofMesh> mesh_list;
};
ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofSetFrameRate(60);
ofSetWindowTitle("openFrameworks");
ofBackground(239);
ofSetColor(255);
ofEnableDepthTest();
this->cap.open("D:\\video\\image51.mp4");
this->cap_size = cv::Size(256, 144);
this->image.allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_COLOR);
this->frame = cv::Mat(cv::Size(this->image.getWidth(), this->image.getHeight()), CV_MAKETYPE(CV_8UC3, this->image.getPixels().getNumChannels()), this->image.getPixels().getData(), 0);
this->number_of_frames = 25 * 27;
for (int i = 0; i < this->number_of_frames; i++) {
cv::Mat src, tmp;
this->cap >> src;
if (src.empty()) {
return;
}
cv::resize(src, tmp, this->cap_size);
cv::cvtColor(tmp, tmp, cv::COLOR_BGR2RGB);
this->frame_list.push_back(tmp);
}
for (int i = 0; i < 128; i++) {
ofMesh mesh;
mesh.addVertex(glm::vec3(-128, -72, 0));
mesh.addVertex(glm::vec3(128, -72, 0));
mesh.addVertex(glm::vec3(128, 72, 0));
mesh.addVertex(glm::vec3(-128, 72, 0));
mesh.addTexCoord(glm::vec3(0, 0, 0));
mesh.addTexCoord(glm::vec3(256, 0, 0));
mesh.addTexCoord(glm::vec3(256, 144, 0));
mesh.addTexCoord(glm::vec3(0, 144, 0));
mesh.addIndex(0); mesh.addIndex(1); mesh.addIndex(2);
mesh.addIndex(0); mesh.addIndex(2); mesh.addIndex(3);
this->mesh_list.push_back(mesh);
}
}
//--------------------------------------------------------------
void ofApp::update() {
ofSeedRandom(39);
}
//--------------------------------------------------------------
void ofApp::draw() {
this->cam.begin();
ofRotateX(180);
ofRotateY(ofGetFrameNum() * 0.5);
for (auto& mesh : this->mesh_list) {
auto location = glm::vec3(ofRandom(-500, 500), ofRandom(-500, 500), ofRandom(-500, 500));
//int n = ofMap(ofNoise(glm::vec4(location, ofGetFrameNum() * 0.005)), 0, 1, 0, this->number_of_frames);
int n = int(ofRandom(1000) + ofGetFrameNum()) % this->number_of_frames;
this->frame_list[n].copyTo(this->frame);
this->image.update();
ofPushMatrix();
ofTranslate(location);
this->image.bind();
mesh.draw();
this->image.unbind();
ofPopMatrix();
}
this->cam.end();
}
main.cpp
#include "ofApp.cpp"
#include "ofApp.h"
//========================================================================
int main() {
ofSetupOpenGL(1280, 720, OF_WINDOW);
ofRunApp(new ofApp());
}
I get 2 errors: "No matching function for call to 'ofRunApp'" and "No type named 'VideoCapture' in namespace 'cv'"
I have sourced the code from https://github.com/junkiyoshi/Insta20200217 as I am trying to learn and develop an art project similar to this. Any help will be very much appreciated
Please add #include "highgui.h" to your main.cpp

unique_ptr incomplete type for forward declaration with default destructor

My question is quite related to this thread:
std::unique_ptr with an incomplete type won't compile
I understood the concept of incomplete type with unique_ptr and need to provide the default destructor for the holding class. However, I'm still getting following error:
invalid use of incomplete type ‘class Game::Sprite’
Following is my simplified code. Please let me know if I'm still missing something here? :
/*
* Game.h
*/
#pragma once
#include <string>
#include <unordered_map>
#include "SDL.h"
class Game {
public:
Game();
~Game();
void run();
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
private:
void setup();
void loadResources();
void createWorld();
void loop();
window mWindow;
renderer mRenderer;
class Sprite;
using sprite = std::unique_ptr<Sprite>;
sprite mPlayer;
struct Resource {
std::string name;
std::string path;
};
enum class ResourceType {
TEXTURE, IMAGE, AUDIO
};
std::unordered_map<ResourceType, Resource> mResourceList;
std::unordered_map<std::string, texture> mTextureHolder;
};
/*
* Game.cpp
*/
#include <Game.h>
...
#include "Sprite.h"
Game::Game() {
...
}
void Game::run() {
setup();
loadResources();
loop();
}
void Game::setup() {
...
}
void Game::loadResources() {
...
}
void Game::createWorld() {
...
}
void Game::loop() {
bool stop = false;
SDL_Event e;
while (!stop) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
stop = true;
}
}
//clear screen
SDL_RenderClear(mRenderer.get());
//render texture
// this gives error:
mPlayer->renderFrame(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0, 0);
//update screen
SDL_RenderPresent(mRenderer.get());
}
}
//default destructor (tried explicit implmentation too)
Game::~Game() = default;
/*
* Sprite.h
*/
#pragm once
#include "SDL.h"
#include <cstdint>
class Sprite {
public:
Sprite(renderer renderer, texture texture, uint32_t width, uint32_t height);
void renderFrame(uint32_t dstX, uint32_t dstY, uint32_t offsetX,
uint32_t offsetY);
void setTexture(texture texture);
private:
renderer mRenderer;
texture mTexture;
uint32_t mWidth;
uint32_t mHeight;
};
/*
* Sprite.cpp
*/
#include "Sprite.h"
Sprite::Sprite(renderer renderer, texture texture, uint32_t width,
uint32_t height) :
mRenderer(renderer), mTexture(std::move(texture)), mWidth(width), mHeight(
height) {
}
void Sprite::renderFrame(uint32_t dstX, uint32_t dstY, uint32_t offsetX,
uint32_t offsetY) {
...
}
void Sprite::setTexture(texture texture) {
...
}

qt graphicsscene line subclass

I added a Line subclass from the GraphicsScene class , to draw a line. From MainWindow I call the line function in that class, there are no errors, but the line isn't drawn. I know this must be lack of my c++ skills. But searching didn't help me on this one. What I want is to make different classes for drawing different shapes, instead of polluting the GraphicsScene with all that code, to keep things a bit structured. But what am I doing wrong? I posted my code on github github.com/JackBerkhout/QT_Draw001
line.h
#ifndef LINE_H
#define LINE_H
#include <QMainWindow>
#include <QObject>
#include <QDebug>
#include "graphicsscene.h"
class Line: public GraphicsScene
{
public:
Line();
void drawLine(int x1, int y1, int x2, int y2);
};
#endif // LINE_H
line.cpp
#include "line.h"
Line::Line():
GraphicsScene()
{
}
void Line::drawLine(int x1, int y1, int x2, int y2)
{
// Just draw something by clicking a button
qDebug() << "line"; // This debug message is shown
QColor color;
color.setRgb(128, 0, 255);
QPen pen;
pen.setColor(color);
pen.setWidth(20);
pen.setCapStyle(Qt::RoundCap);
this->addLine(x1, y1, x2, y2, pen); // Didn't draw the line on the scene
}
graphicsscene.cpp
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QList>
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit GraphicsScene(QObject *parent = 0);
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent);
QPointF getMousePoint(void);
int getMouseX(void);
int getMouseY(void);
int getNumber(void);
void setNumber(int num);
QPointF mousePoint;
int MouseX, MouseY;
int myNumber;
signals:
void changedMousePosition(QPointF mousePoint);
void changedNumber(int myNumber);
public slots:
private:
QList <QPointF> mousePoints;
// int Number;
};
#endif // GRAPHICSSCENE_H
graphicsscene.cpp
#include "mainwindow.h"
#include "graphicsscene.h"
#include <QDebug>
GraphicsScene::GraphicsScene(QObject *parent) :
QGraphicsScene(parent)
{
this->setBackgroundBrush(Qt::black);
myNumber = 0;
// this-> ->setMouseTracking(true);
}
void GraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
// mousePoint = mouseEvent->scenePos();
// MouseX = mousePoint.x();
// MouseY = mousePoint.y();
qDebug() << Q_FUNC_INFO << mouseEvent->scenePos();
QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
}
void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
mousePoint = mouseEvent->scenePos();
MouseX = mouseEvent->scenePos().x();
MouseY = mouseEvent->scenePos().y();
emit changedMousePosition(mousePoint);
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
mousePoint = mouseEvent->scenePos();
MouseX = mouseEvent->scenePos().x();
MouseY = mouseEvent->scenePos().y();
mousePoints.append(mouseEvent->scenePos());
MainWindow *mainWindow = new MainWindow();
mainWindow->Count++;
if(mousePoints.size() == 2)
{
myNumber++;
emit changedNumber(myNumber);
QColor color;
color.setRgb(128, 0, 255);
QPen pen;
pen.setColor(color);
pen.setWidth(20);
pen.setCapStyle(Qt::RoundCap);
this->addLine(mousePoints.at(0).x(), mousePoints.at(0).y(), mousePoints.at(1).x(), mousePoints.at(1).y(), pen);
mousePoints.clear();
}
QGraphicsScene::mousePressEvent(mouseEvent);
}
void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
mousePoint = mouseEvent->scenePos();
MouseX = mouseEvent->scenePos().x();
MouseY = mouseEvent->scenePos().y();
QGraphicsScene::mouseReleaseEvent(mouseEvent);
}
QPointF GraphicsScene::getMousePoint(void)
{
return mousePoint;
}
int GraphicsScene::getMouseX(void)
{
MouseX = mousePoint.x();
return mousePoint.x();
}
int GraphicsScene::getMouseY(void)
{
MouseY = mousePoint.y();
return mousePoint.y();
}
void GraphicsScene::setNumber(int num)
{
myNumber = num;
}
int GraphicsScene::getNumber(void)
{
return myNumber;
}
You are complicating too much, besides that I think you do not understand what is the purpose of the inheritance, you just have to create a function in GraphicsScene called drawLine and use it when you need it.
GraphicsScene.h
public:
void drawLine(int x1, int y1, int x2, int y2);
GraphicsScene.cpp
void GraphicsScene::drawLine(int x1, int y1, int x2, int y2)
{
QColor color;
color.setRgb(128, 0, 255);
QPen pen;
pen.setColor(color);
pen.setWidth(20);
pen.setCapStyle(Qt::RoundCap);
addLine(x1, y1, x2, y2, pen);
}
And then you call in Mainwindow:
void MainWindow::on_toolButtonDraw_clicked()
{
scene->drawLine(300, 100, 500, 300);
}
When you create a Line object you are creating a new scene and it will be drawn in that scene, so you will not see it.

SDL 2.0 C++: Polymorphism Vectors, my objects are not loading different images

So I'm basically following a book called SDL Game Development by Shaun Mitchell.
I'm current on page 58, where it tells us how to implement polymorphism.
So far what I have down are:
- Create the main code where we do all our rendering updating etc. (Game.cpp and Game.h)
- Create a Texture manager as a singleton (TextureManager.h and TextureManager.cpp)
- Create a GameObject (GameObject.h and GameObject.cpp)
- Create a Player that inherits GameObject (Player.h and Player.cpp)
So my GameObject class basically has 3 methods: load, draw and update.
Load basically loads an image, and I use my TextureManager to do so.
Draw draws it and update just changes it positions and current frame.
I pass the name of the image which is basically the directory, the renderer and the textureID from the Game class where I call them.
Essentially the problem is that I pass in two different directories into a GameObject
and a Player, but it only loads up the last directory I pass in. As in both GameObject and Player becomes the same image.
So I think the problem lies in Game class, Player class or GameObject class.
I create the objects in Game class, and push them in vector at Game.cpp in init function
Where it says
m_go->load(100, 100, 96, 60, "assets/simba.bmp", "animate",m_pRenderer);
m_player->load(300, 300, 96, 60, "assets/download.bmp", "animate",m_pRenderer);
GameObject.h
#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
#include <string>
#include <iostream>
#include "SDL_image.h"
#include "TextureManager.h"
class GameObject
{
public:
virtual bool load(int x, int y, int width, int height, std::string name, std::string textureID, SDL_Renderer* pRenderer);
virtual void draw(SDL_Renderer* pRenderer);
virtual void update();
virtual void clean();
protected:
std::string m_textureID;
int m_currentFrame;
int m_currentRow;
int m_x;
int m_y;
int m_width;
int m_height;
};
#endif // GAMEOBJECT_H
GameObject.cpp
#include "GameObject.h"
bool GameObject::load(int x, int y, int width, int height, std::string name, std::string textureID, SDL_Renderer* pRenderer)
{
m_x = x;
m_y = y;
m_width = width;
m_height = height;
m_textureID = textureID;
m_currentRow = 1;
m_currentFrame = 1;
if(!TextureManager::Instance()->load(name, textureID, pRenderer))
{
return false;
}
return true;
}
void GameObject::draw(SDL_Renderer* pRenderer)
{
TextureManager::Instance()->drawFrame(m_textureID, m_x, m_y,m_width, m_height, m_currentRow, m_currentFrame, pRenderer);
}
void GameObject::update()
{
m_x += 1;
m_currentFrame = int(((SDL_GetTicks() / 100) % 6));
}
void GameObject::clean()
{
}
In my Player class, I do the same thing except I use the methods that are already made in GameObject
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include "GameObject.h"
class Player : public GameObject
{
public:
bool load(int x, int y, int width, int height, std::string name, std::string textureID, SDL_Renderer* pRenderer);
void draw(SDL_Renderer* pRenderer);
void update();
void clean();
};
#endif // PLAYER_H
Player.cpp
#include "Player.h"
bool Player::load(int x, int y, int width, int height, std::string name, std::string textureID, SDL_Renderer* pRenderer)
{
if(!GameObject::load(x, y, width, height, name, textureID, pRenderer))
{
return false;
}
return true;
}
void Player::draw(SDL_Renderer* pRenderer)
{
GameObject::draw(pRenderer);
}
void Player::update()
{
m_currentFrame = int(((SDL_GetTicks() / 100) % 6));
m_x -= 1;
}
void Player::clean()
{
}
This is the part where the images doesn't load the same thing happens in Game.cpp
So I basically created a vector that holds GameObjects, it told me to use it so that I could just for loop through it whenever I want to draw objects
Game.h
#ifndef GAME_H
#define GAME_H
#include <SDL.h>
#include <SDL_image.h>
#include <string.h>
#include <iostream>
#include <vector>
#include "TextureManager.h"
#include "Player.h"
class Game
{
public:
Game() {}
~Game() {}
bool init(const char* title, int xpos, int ypos, int width, int height, int flags);
void render();
void update();
void handleEvents();
void clean();
// a function to access the private running variable
bool running() { return m_bRunning; }
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
int m_currentFrame;
bool m_bRunning;
GameObject* m_go;
GameObject* m_player;
std::vector<GameObject*> m_gameObjects;
};
#endif
Game.cpp
#include "Game.h"
typedef TextureManager TheTextureManager; // Singleton TextureManager
Game* g_game = 0; // Game Object
bool Game::init(const char* title, int xpos, int ypos, int width,int height, int flags)
{
// attempt to initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "SDL init success\n";
// init the window
m_pWindow = SDL_CreateWindow(title, xpos, ypos,width, height, flags);
if(m_pWindow != 0) // window init success
{
std::cout << "window creation success\n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer != 0) // renderer init success
{
std::cout << "renderer creation success\n";
SDL_SetRenderDrawColor(m_pRenderer,255,255,255,255);
}
else
{
std::cout << "renderer init fail\n";
return false; // renderer init fail
}
}
else
{
std::cout << "window init fail\n";
return false; // window init fail
}
}
else
{
std::cout << "SDL init fail\n";
return false; // SDL init fail
}
std::cout << "init success\n";
m_go = new GameObject();
m_player = new Player();
m_go->load(100, 100, 96, 60, "assets/simba.bmp", "animate",m_pRenderer);
m_player->load(300, 300, 96, 60, "assets/download.bmp", "animate",m_pRenderer);
m_gameObjects.push_back(m_go);
m_gameObjects.push_back(m_player);
m_bRunning = true; // everything inited successfully, start the main loop
return true;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color
//m_go.draw(m_pRenderer);
//m_player.draw(m_pRenderer);
for(std::vector<GameObject*>::size_type i = 0; i != m_gameObjects.size(); i++)
{
m_gameObjects[i]->draw(m_pRenderer);
}
SDL_RenderPresent(m_pRenderer); // draw to the screen
}
void Game::clean()
{
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
void Game::handleEvents()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
m_bRunning = false;
break;
default:
break;
}
}
}
void Game::update()
{
//m_currentFrame = int(((SDL_GetTicks() / 100) % 6));
//m_go.update();
//m_player.update();
for(std::vector<GameObject*>::size_type i = 0; i != m_gameObjects.size(); i++)
{
m_gameObjects[i]->update();
}
}
int main(int argc, char* argv[])
{
g_game = new Game();
g_game->init("Chapter 1", 100, 100, 640, 480, 0);
while(g_game->running())
{
g_game->render();
g_game->handleEvents();
g_game->update();
SDL_Delay(10); // add the delay
}
g_game->clean();
return 0;
}
As you can see at the end of the init function, I have m_go load up simba, and m_player load up download. Even though they move in different directions (GameObject moves to the right and Player moves to the left as you can see in the classes in update method).
My program only loads download for both.
I'm not sure if the TextureManager class is related but I'll post it anyway
TextureManager.h
#ifndef TEXTUREMANAGER_H
#define TEXTUREMANAGER_H
#include <SDL.h>
#include <SDL_image.h>
#include <string.h>
#include <iostream>
#include <map>
class TextureManager
{
public:
bool load(std::string fileName,std::string id,SDL_Renderer* pRenderer);
void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);
void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);
std::map <std::string, SDL_Texture*> m_textureMap;
static TextureManager* Instance()
{
if(s_pInstance == 0)
{
s_pInstance = new TextureManager();
return s_pInstance;
}
return s_pInstance;
}
//typedef TextureManager TheTextureManager;
protected:
private:
TextureManager(){}
static TextureManager* s_pInstance;
};
#endif // TEXTUREMANAGER_H
TextureManager.cpp
#include "TextureManager.h"
TextureManager* TextureManager::s_pInstance = 0;
bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer)
{
SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());
if(pTempSurface == 0)
{
return false;
}
SDL_Texture* pTexture =
SDL_CreateTextureFromSurface(pRenderer, pTempSurface);
SDL_FreeSurface(pTempSurface);
// everything went ok, add the texture to our list
if(pTexture != 0)
{
m_textureMap[id] = pTexture;
return true;
}
// reaching here means something went wrong
return false;
}
void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = 0;
srcRect.y = 0;
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x = x;
destRect.y = y;
SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect,
&destRect, 0, 0, flip);
}
void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = width * currentFrame;
srcRect.y = height * (currentRow - 1);
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x = x;
destRect.y = y;
SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect,
&destRect, 0, 0, flip);
}
You are using "animate" more than once for the textureID - this needs to be unique
Your Code:
m_go->load(100, 100, 96, 60, "assets/simba.bmp", "animate", m_pRenderer);
m_player->load(300, 300, 96, 60, "assets/download.bmp", "animate", m_pRenderer)
What happens is the texture is loaded but it overwrites the existing texture which has the "animate" key in the map. Make them unique and this should resolve your issue.