Reputation: 1
I have some Java game dev experience and this is my first attempt at some C++ game dev.
This game is supposed to be a very simple 2d Game a sort of whack'a'mole but with bubbles.
It seems as though I'm not rendering my Sprites correctly so this happens:
How the texture should look like
I read this so I resized my texture to 400x400px but that didn't fix anything.
Does this have to do with the depth? or with blending? what am I doing wrong?
Main:
#include "Engine\Engine.h"
#include <iostream>
#include "Engine/Graphics/Sprite.h"
using namespace std;
int main()
{
Engine engine;
engine.Initialize("Bursting!");
Sprite testSprite = Sprite("Assets/Art/Bubble.png", 100,100);
while (true)
{
engine.Update();
testSprite.Update();
engine.BeginRender();
testSprite.Render();
engine.EndRender();
}
return 0;
}
Engine.cpp:
#include "Engine.h"
int Engine::SCREEN_WIDTH = 1024;
int Engine::SCREEN_HEIGHT = 768;
GLFWwindow* Engine::window = NULL;
Engine::Engine()
{
}
Engine::~Engine()
{
}
bool Engine::Initialize(char* windowTitle)
{
if (!glfwInit())
{
cout << "Error initializing GLFW" << endl;
return false;
}
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, windowTitle, NULL, NULL);
if (window == NULL)
{
cout << "Error creating window " << endl;
return false;
}
//OpenGL Setup
glfwMakeContextCurrent(window);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glfwSwapInterval(1);
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
int xPos = (mode->width - SCREEN_WIDTH) / 2;
int xyPos = (mode->height - SCREEN_HEIGHT) / 2;
//GL Setup
//Viewport
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -32, 32);
glDepthRange(-32, 32);
glMatrixMode(GL_MODELVIEW);
//Alpha Blending
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return true;
}
void Engine::Update()
{
glfwPollEvents();
}
void Engine::BeginRender()
{
glClearColor(0, 0, 0, 0); //clear back buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
//this swaps buffers, anything that needs to be drawn and seen by human eyes needs to happen before this func
void Engine::EndRender()
{
glfwSwapBuffers(window);
}
Texture.cpp:
#include "Texture.h"
Texture::Texture()
{
id = -1; //Opengl tracks our texture as, this allows us to set texture before drawing
}
Texture::Texture(int _id)
{
id = _id;
if (!GetTextureParams())
{
cout << "Error loading Image with ID: " << id << endl;
}
}
Texture::Texture(string path)
{
id = SOIL_load_OGL_texture(path.c_str(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MULTIPLY_ALPHA);
if (!GetTextureParams())
{
cout << "Error loading Image from path: " << path << endl;
}
}
int Texture::getId()
{
return id;
}
int Texture::getWidth()
{
return width;
}
int Texture::getHeight()
{
return height;
}
bool Texture::GetTextureParams()
{
if (id > 0)
{
int mipLevel = 0;
glBindTexture(GL_TEXTURE_2D, id);
glGetTexLevelParameteriv(GL_TEXTURE_2D, mipLevel, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, mipLevel, GL_TEXTURE_HEIGHT, &height);
return true;
}
return false;
}
Sprite.cpp:
#include "Sprite.h"
Sprite::Sprite()
{
xPos = 0;
yPos = 0,
texture = Texture();
}
Sprite::Sprite(string imgPath)
{
texture = Texture(imgPath);
xPos = 0;
yPos = 0;
}
Sprite::Sprite(string imgPath, float _xPos, float _yPos)
{
texture = Texture(imgPath);
xPos = _xPos;
yPos = _yPos;
}
void Sprite::Update()
{
}
void Sprite::Render()
{
glEnable(GL_TEXTURE_2D),
glBindTexture(GL_TEXTURE_2D, texture.getId());
glLoadIdentity();
//TRANS -> ROT -> SCALE
glTranslatef(xPos, yPos, 0);
//rendering
glColor4f(1, 1, 1, 1);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(texture.getWidth(), 0);
glTexCoord2f(0, 1); glVertex2f(0 , texture.getHeight());
glTexCoord2f(1, 1); glVertex2f(texture.getWidth(), texture.getHeight());
glEnd();
glDisable(GL_TEXTURE_2D);
}
Upvotes: 0
Views: 153
Reputation: 1
I was building it wrong or importing it wrong. i.e. Lines were in the wrong order:
in my sprite.cpp my render() looked like:
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(texture.getWidth(), 0);
glTexCoord2f(0, 1); glVertex2f(0 , texture.getHeight()); //error
glTexCoord2f(1, 1); glVertex2f(texture.getWidth(), texture.getHeight());
glEnd();
glDisable(GL_TEXTURE_2D);
I changed the order :
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(texture.getWidth(), 0);
glTexCoord2f(1, 1); glVertex2f(texture.getWidth(), texture.getHeight());
glTexCoord2f(0, 1); glVertex2f(0 , texture.getHeight()); //correct position
glEnd();
glDisable(GL_TEXTURE_2D);
And it works now! Thanks.
Upvotes: 0