I'm trying to render a SDL_Rect in a class, I send the renderer to the "draw" function of my class, and then render it from there.
void Tray::draw(SDL_Renderer *renderer){
SDL_RenderFillRect(renderer, &bckRect);
}
When I put the same code on my main loop. All goes fine, but when I do this from my tray class, It compiles, but doesn't run.
Simplified code:
main.cpp
#include <iostream>
#include <SDL2/SDL.h>
// local includes
#include "editor.h"
using namespace std;
int main(int argc, const char * argv[]) {
Editor *editor = new Editor();
while (editor->running){
editor->render();
}
editor->clean();
return 0;
}
editor.h
#ifndef EDITOR_H
#define EDITOR_H
#include <iostream>
#include <SDL2/SDL.h>
#include "Gui-classes/tray.h"
#include "properties.h"
class Editor{
private:
// Window variables
SDL_Window *window;
SDL_Renderer *renderer;
Tray *tray;
public:
Editor();
~Editor();
void render();
};
#endif
editor.cpp
#include "editor.h"
Editor::Editor(){
props = new Properties();
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
window = SDL_CreateWindow("Great Editor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 600, SDL_WINDOW_RESIZABLE);
running = true;
if (window) {
std::cout << "Window created!" << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, 0);
}
}
void Editor::render(){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 12, 12, 55, 255);
tray->draw(renderer);
SDL_RenderPresent(renderer);
}
void Editor::clean(){
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout << "Window ended" << std::endl;
}
tray.h
#ifndef TRAY_H
#define TRAY_H
#include <iostream>
#include <SDL2/SDL.h>
class Tray{
private:
// Position variables
int x;
int y;
int w;
int h;
//Display variables
SDL_Color color;
SDL_Rect bckRect;
public:
Tray();
~Tray();
void draw(SDL_Renderer *renderer);
};
#endif
Tray.cpp
#include "tray.h"
Tray::Tray(){
bckRect.x = x;
bckRect.x = y;
bckRect.w = w;
bckRect.h = h;
}
Tray::~Tray(){
}
void Tray::draw(SDL_Renderer *renderer){
SDL_RenderFillRect(renderer, &bckRect);
//std::cout << "teeest" << std::endl;
}
Thanks for the help.
Related
I have .dll file with my Game Engine and .exe with game.
.exe uses .dll
Both of them use glfw_mt.lib.
In dll There is Window class (Window.cpp,Window.h) where i try to glfwCreateWindow(), but it returns NULL. HOWEVER when i try it in dlls main function (EntryPoint.h) glfwCreateWindow() works fine.
My Window abstractionm fails to create glfw window. I see no reason
EntryPoint.h in dll
#include "Core.h"
#include "Game.h"
#include "Window.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#ifdef NK_PLATFORM_WINDOWS
#define NIKSON_START Nikson::Game* CreateGame(){return
#define NIKSON_END }
extern NIKSON_API Nikson::Game* CreateGame();
static void callback(int error_code, const char* description) {
std::cout << "GLFW ERROR:" << error_code << " " << description << std::endl;
}
int main()
{
LOG("LOADED!");glfwSetErrorCallback(callback);
if (!glfwInit())
{
LOG("GLFW ERROR");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//GLFWwindow* win = glfwCreateWindow(1000, 1000, "Nikson", NULL, NULL);
// LOG(win)
//
///*if (win==nullptr) {
// LOG("FAILED TO CREATE WINDOW")
// return 0;
//}*/
//glfwMakeContextCurrent(win);
Nikson::Window* win = Nikson::CreateWindow("Nikson",1000,1000);
std::cin.get();
//Nikson::Game * game= CreateGame();
////Nikson::Window* win = new Nikson::Window();
//game->Run();
delete win;
}
#endif
Window.h
#pragma once
#include "Core.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
namespace Nikson {
struct WindowProp {
std::string Title;
uint32_t Width;
uint32_t Height;
GLFWwindow* WindowPtr;
};
class NIKSON_API Window
{
public:
Window(GLFWwindow* window, const std::string& title = "Nikson",
uint32_t width = 1600,
uint32_t height = 900);
void Tick();
private:
WindowProp m_Properities;
};
NIKSON_API Nikson::Window* CreateWindow(const std::string& title = "Nikson",
uint32_t width = 1600,
uint32_t height = 900);
}
Window.cpp
#include "Window.h"
namespace Nikson
{
Window::Window(GLFWwindow* window, const std::string& title,
uint32_t width,
uint32_t height)
{
m_Properities.Title = title;
m_Properities.Width=width;
m_Properities.Height=height;
m_Properities.WindowPtr = window;
}
void Window::Tick() {
glfwPollEvents();
}
Nikson::Window* CreateWindow(const std::string& title,
uint32_t width,
uint32_t height)
{
GLFWwindow* win = glfwCreateWindow(1000,1000,"Nikson", NULL, NULL);
if (win == NULL) {
LOG(win)
}
/*if (win==nullptr) {
LOG("FAILED TO CREATE WINDOW")
return 0;
}*/
glfwMakeContextCurrent(win);
return new Nikson::Window(win, title, width, height);
}
}
I am trying to make a simple racing game with C++ using SDL2. When i tried rendering my car asset(PNG Image) it simply did not render saying in the console that it couldn't access the file. Note that the source files are located in an exclusive directory (src/), the headers are in /include and the images are located in /assets. Here is my code:
main.cpp
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include <Headers/RenderWindow.hpp>
int main(int argc, char *args[])
{
if (SDL_Init(SDL_INIT_VIDEO) > 0)
{
std::cout << "SDL_Init has failed..." << std::endl;
}
if (!(IMG_Init(IMG_INIT_PNG)))
{
std::cout << "IMG_Init has failed" << std::endl;
}
RenderWindow window("Untitled Racing Game", 1280, 720);
SDL_Texture *blueCar = window.LoadTexture("../assets/blue_car.png");
bool run = true;
SDL_Event event;
while (run)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
run = false;
}
}
window.Clear();
window.Render(blueCar);
window.Display();
}
window.CleanUp();
SDL_Quit();
return 0;
}
RenderWindow.hpp
#pragma once
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
class RenderWindow
{
public:
RenderWindow(const char *title, int width, int height);
SDL_Texture *LoadTexture(const char *filePath);
void CleanUp();
void Clear();
void Render(SDL_Texture *texture);
void Display();
private:
SDL_Window *window;
SDL_Renderer *renderer;
};
RenderWindow.cpp
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include "Headers/RenderWindow.hpp"
RenderWindow::RenderWindow(const char *title, int width, int height)
: window(NULL), renderer(NULL)
{
window = (SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN));
if (window == NULL)
{
std::cout << "Window Failed to Load. Error " << SDL_GetError() << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
}
SDL_Texture *RenderWindow::LoadTexture(const char *filePath)
{
SDL_Texture *texture = NULL;
texture = IMG_LoadTexture(renderer, filePath);
if (texture == NULL)
{
std::cout << "Failed to load texture, " << SDL_GetError() << std::endl;
}
return texture;
}
void RenderWindow::CleanUp()
{
SDL_DestroyWindow(window);
}
void RenderWindow::Clear()
{
SDL_RenderClear(renderer);
}
void RenderWindow::Render(SDL_Texture *texture)
{
SDL_RenderCopy(renderer, texture, NULL, NULL);
}
void RenderWindow::Display()
{
SDL_RenderPresent(renderer);
}
I have been trying to make a pong clone as a first c++ "big" project, and I am encountering several problems.
First of all, this is my code so far:
game.hpp:
#pragma once
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
class Game
{
public:
Game();
~Game();
// GAME VARIABLES
const int WIDTH=720, HEIGHT=720, FONT_SIZE=32;
int running, lastTime;
int frameCount, timerFPS, lastFrame, fps;
// GAME TOOLS
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *font;
SDL_Color color;
// GAME OBJECTS:
SDL_Rect rPaddle, lPaddle, Ball, scoreBoard;
// GAME FUNCTIONS:
void initRects(); // AFTER YOU HAVE A WORKING VERSION, TRY TO MOVE TO CONSTRUCTOR
void render();
void run();
};
game.cpp:
#include "game.hpp"
Game::Game()
{
running = 1;
if(SDL_Init(SDL_INIT_EVERYTHING)<0)
std::cerr << "SDL FAILED: SDL_INIT_EVERYTHING: " << SDL_GetError() << std::endl;
if(SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer)<0)
std::cerr << "SDL FAILED: SDL_CreateWindowAndRenderer: " << SDL_GetError() << std::endl;
TTF_Init();
font = TTF_OpenFont("PreschoolBits.ttf", FONT_SIZE);
color.r=color.g=color.b=255;
}
Game::~Game()
{
TTF_CloseFont(font);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
}
void Game::initRects()
{
lPaddle.x=32; lPaddle.h=HEIGHT/5;
lPaddle.y=(HEIGHT/2)-(lPaddle.h/2);
lPaddle.w=12;
rPaddle=lPaddle;
rPaddle.x=WIDTH-rPaddle.w-32;
Ball.w=Ball.h=16;
}
void Game::render()
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
frameCount++;
timerFPS=SDL_GetTicks()-lastFrame;
if(timerFPS<(1000/60))
SDL_Delay(timerFPS<((1000/60)-timerFPS));
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0);
SDL_RenderFillRect(renderer, &lPaddle);
SDL_RenderFillRect(renderer, &rPaddle);
SDL_RenderFillRect(renderer, &Ball);
SDL_RenderPresent(renderer);
}
void Game::run()
{
initRects();
while(running)
{
lastFrame=SDL_GetTicks();
if(lastFrame>=(lastTime+1000))
{
lastTime=lastFrame;
fps=frameCount;
frameCount=0;
}
render();
SDL_Delay(2000);
running=0;
}
}
pong.cpp:
#include "game.hpp"
int main()
{
Game game;
game.run();
return 0;
}
Now, when I try to render the render the rectangles, they don't show up. So I tried to remove them and just render colors on screen.
Both before and after the SDL_RenderClear I wrote:
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
But the window still showed up black, no matter what I tried.
What could I be doing wrong?
To make a filled Rect you must create a rect object:SDL_Rect MyRectName, Set its values to whatever you want:MyRectName.(w,h,x,y) = MyValue, and then to render, run SDL_RenderFillRect(MyRenderer, MyRectName);.
I am currently learning SDL2 and just managed to create a ping pong game. Some friends told me that I should start using classes for managing as an example player1 and player2. I know how to create a class but I do not understand how I would be able to pass SDL_Renderer between classes in order to render an object from within the class to the main.cpp file.
#include "SDL2/SDL.h"
#include "SDL2/SDL_render.h"
#include <iostream>
#include <windows.h>
#include <thread>
#include "player.h"
Player Player; //defining the class
const int WINDOW_WIDTH = 1280;
const int WINDOW_HEIGHT = 720;
int main(int argc, char *args[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window;
SDL_Renderer *renderer;
window = SDL_CreateWindow ("Test",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
0
);
if (window == NULL) {
std::cout << "Window could not load" << SDL_GetError() << std::endl;
return 0;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
while (running) //running is a bool (true) {
Player.draw();
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
What do I have to do in my player.cpp draw function in order to draw an object on the screen?
Using C++ windows, compiling the code with g++ main.cpp player.cpp -o main.exe -IC:\MinGW\i686-w64-mingw32\include -LC:\MinGW\i686-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2
In main.cpp
Player.draw(renderer)
(presuming you are using the same code as above, just pass "renderer" to the Player.draw() function.)
In player.h
class Player {
public:
void draw(SDL_Renderer *renderer)
};
In player.cpp
void Player::draw(SDL_Renderer *renderer) {
SDL_Rect object;
object.x = 0;
object.y = 0;
object.h = 10;
object.w = 10;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawRect(renderer, &object);
SDL_RenderFillRect(renderer, &object);
}
// will draw a white box with at position (0,0)
I believe this is a simple way to do it
It was working before I tried to load my image.
This is the error I get:
Error 1 error LNK2005: "struct SDL_Window * m_pWindow"
(?m_pWindow##3PAUSDL_Window##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 2 error LNK2005: "struct SDL_Renderer * m_pRenderer"
(?m_pRenderer##3PAUSDL_Renderer##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 3 error LNK2005: "struct SDL_Texture * m_pTexture"
(?m_pTexture##3PAUSDL_Texture##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 4 error LNK2005: "struct SDL_Rect m_sourceWREK"
(?m_sourceWREK##3USDL_Rect##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 5 error LNK2005: "struct SDL_Rect m_destWREK"
(?m_destWREK##3USDL_Rect##A) already defined in
Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj
Error 7 error LNK1169: one or more multiply defined symbols found
C:\Users\Joseph\Desktop\DuckGotti\Debug\DuckGotti.exe 1
Here is my code (main.cpp):
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "Game.h"
/**my first game obj**/
Game* g_game = 0;
int main(int argc, char* argv[])
{
g_game = new Game();
g_game->init("Duck Gotti", 100, 100, 640, 480, SDL_WINDOW_FULLSCREEN );
while (g_game->running())
{
g_game->handelEvents();
g_game->update();
g_game->render();
}
g_game->clean();
return 0;
}
Game.h
#ifndef __Game__
#define __Game__
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <iostream>
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
SDL_Texture* m_pTexture;
SDL_Rect m_sourceWREK;
SDL_Rect m_destWREK;
class Game
{
public:
Game(){}
~Game(){}
/**setting the run var to true**/
bool init(const char* title, int xpos, int ypos, int width, int height
,bool fullscreen);
void render();
void update();
void handelEvents();
void clean();
/** a func to access the privet var **/
bool running() { return m_bRunning; }
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
bool m_bRunning;
};
#endif /**defined (__Game__) **/
Game.cpp:
#include "game.h"
bool Game::init(const char* title, int xpos, int ypos, int width,
int height, bool fullscreen )
{
int flags = 0;
if (fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
/**attempt to initalize SDL**/
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "SDL init sucess/n";
/**init the winn**/
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (m_pWindow !=0)
{
std::cout << "window creation sucess/n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if (m_pRenderer != 0)
{
std::cout << "renderer created/n";
SDL_SetRenderDrawColor(m_pRenderer, 255, 255, 255, 255);
}
else
{
std::cout << "renderer broked/n";
return false;
}
}
else
{
std::cout << "winn init nope/n";
return false;
}
}
else
{
std::cout << "SDL init broked/n";
return false;
}
std::cout << "init sucess/n";
m_bRunning = true;
return true;
SDL_Surface* pTempSurf = SDL_LoadBMP("img3.bmp");
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurf);
SDL_FreeSurface(pTempSurf);
SDL_QueryTexture(m_pTexture, NULL, NULL, &m_sourceWREK.w, &m_destWREK.h);
m_destWREK.x = m_sourceWREK.x = 0;
m_destWREK.y = m_sourceWREK.y = 0;
m_destWREK.w = m_sourceWREK.w;
m_destWREK.h = m_sourceWREK.h;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer);
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceWREK, &m_destWREK);
SDL_RenderPresent(m_pRenderer);
}
void Game::update()
{
}
void Game::handelEvents()
{
SDL_Event evil;
if (SDL_PollEvent(&evil))
{
switch (evil.type)
{
case SDL_QUIT:
m_bRunning = false;
break;
default:
break;
}
}
}
void Game::clean()
{
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}
I believe you are reading this book SDL Game Development. Be aware, the book has a bunch of errors.
remove these lines
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
SDL_Texture* m_pTexture;
SDL_Rect m_sourceWREK;
SDL_Rect m_destWREK;
They are already defined inside game class.
I was reading this book, luckily I was organized and I kept the files. It seems you are in the first chapter
main.cpp
#include "game.h"
Game* g_game = 0;
int main(int argc, char* args[])
{
g_game = new Game();
g_game->init("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
while( g_game->running() ){
g_game->handleEvents();
g_game->update();
g_game->render();
SDL_Delay(10);
}
g_game->clean();
return 0;
}
game.h
#ifndef _GAME_H
#define _GAME_H
#include "SDL.h"
class Game
{
public:
Game() {}
~Game() {}
bool init(const char* title, int xpos, int ypos, int height, int width, int flags);
void render();
void update();
void handleEvents();
void clean();
bool running() { return m_bRunning; }
private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
bool m_bRunning;
};
#endif /* defined(_GAME_H) */
game.cpp
#include "game.h"
#include <iostream>
bool Game::init(const char* title, int xpos, int ypos, int height, int width, int flags)
{
if ( SDL_Init(SDL_INIT_EVERYTHING) == 0 ){
std::cout << "SDL init success\n";
m_pWindow = SDL_CreateWindow(title, xpos, ypos, height, width, flags);
if ( m_pWindow != 0 ){
std::cout << "window creation success \n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if ( m_pRenderer != 0 ){
std::cout << "renderer creation success\n";
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
}else{
std::cout << "renderer init fail\n";
return false;
}
}else{
std::cout << "window init fail\n";
return false;
}
}else{
std::cout << "SDL init fail\n";
return false;
}
std::cout << "init success\n";
m_bRunning = true;
//#####################################################################################
return true;
}
void Game::render()
{
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
SDL_RenderClear(m_pRenderer);
SDL_RenderPresent(m_pRenderer);
}
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()
{
}
These are some notes I've written
After chapter 2, you need to organize your classes because you will get lost easily with the inheritance. For example, the below picture, you will see the way classes are connected in chapter 3
For chapter 4,
Hope this helps.