And I get this:
And I know for a fact it's the "IMG_load(" Function that's wrecking everything.
I've pasted the "paddle.png" file in every folder of my project.
Here's the code:
(comment out the /*
unsigned int pad_texture = 0;
pad_texture = LoadTexture("paddle.png");
std::cout << "Paddle texture: " << pad_texture << std::endl;*/ Part to see the code in action)
/*
*/
/********* INCLUDES *********/
#include "SDL.h"
#include "SDL_opengl.h"
#include "SDL_image.h"
#include <iostream>
#include <string>
/********* Structures *********/
struct Brick
{
float x;
float y;
float width;
float height;
bool alive;
};
/********* GLOBALS *********/
int screen_width = 640;
int screen_height = 420;
// Pad variables
float speed = 10;
float width = 50;
float height = 10;
float myX = height/2 + 300;
float myY = screen_height - height - 30;
// ball variables
float ballX = 50;
float ballY = 350;
float ballDiam = 10;
float velx = 2;
float vely = -2;
// brick stuffs
const static int BRICKS = 45;
Brick bricks[BRICKS];
/********* Function Declarations *********/
void initGraphicStuff();
void Render();
bool checkCollision(float Ax, float Ay, float Aw, float Ah, float Bx, float By, float Bw, float Bh);
GLuint LoadTexture( const std::string &fileName);
/********* MAIN FUNCTIONS *********/
int main(int argc, char *args[])
{
initGraphicStuff();
std::cout << "OpenGL is running.\n";
std::cout << "Main loop has started.\n";
bool isRunning = true;
SDL_Event event;
bool left = false, right = false;
for (int i = 0, x = 20, y = 10; i < BRICKS; i++, x += 68)
{
if (x > screen_width - 40)
{
x = 20;
y += 25;
}
bricks.x = x;
bricks.y = y;
bricks.width = 60;
bricks.height = 20;
bricks.alive = true;
}
bool paddleAI = false;
int prevX;
unsigned int pad_texture = 0;
pad_texture = LoadTexture("paddle.png");
std::cout << "Paddle texture: " << pad_texture << std::endl;
// Main Game loop
while(isRunning)
{
// Events //
while (SDL_PollEvent(&event))
{
// logic that should happen for certain events
if (event.type == SDL_QUIT)
isRunning = false;
// If button is released, and said button is escape, quit
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)
isRunning = false;
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r)
{
paddleAI = !paddleAI;
if (paddleAI)
std::cout << "Paddle AI ON\n";
if (!paddleAI)
std::cout << "Paddle AI OFF\n";
}
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_LEFT)
left = true;
else if (event.key.keysym.sym == SDLK_RIGHT)
right = true;
}
if (event.type == SDL_KEYUP)
{
if (event.key.keysym.sym == SDLK_LEFT)
left = false;
else if (event.key.keysym.sym == SDLK_RIGHT)
right = false;
}
}
// Logic //
if (left == true)
{
myX -= speed;
}
if (right == true)
{
myX += speed;
}
// AI paddle
if (paddleAI)
{
prevX = myX;
myX = ballX + (ballDiam / 2) - (width / 2);
if (myX > prevX + speed)
myX = prevX + speed;
if (myX < prevX - speed)
myX = prevX - speed;
}
if (myX < 0)
myX = 0;
if (myX + width > screen_width)
myX = screen_width - width;
// BRICK COLLISION
ballX += velx;
for (int i = 0; i < BRICKS; i++)
{
if (bricks.alive == true)
{
if ( checkCollision(ballX, ballY, ballDiam, ballDiam, bricks.x, bricks.y, bricks.width, bricks.height) )
{
if (velx > 0)
velx += 0.2;
else
velx -= 0.2;
if (vely > 0)
vely += 0.2;
else
vely -= 0.2;
velx = -velx;
bricks.alive = false;
break;
}
}
}
ballY += vely;
for (int i = 0; i < BRICKS; i++)
{
if (bricks.alive == true)
{
if ( checkCollision(ballX, ballY, ballDiam, ballDiam, bricks.x, bricks.y, bricks.width, bricks.height) )
{
if (velx > 0)
velx += 0.2;
else
velx -= 0.2;
if (vely > 0)
vely += 0.2;
else
vely -= 0.2;
vely = -vely;
bricks.alive = false;
break;
}
}
}
// BALL LOGIC
if (ballX < 0 || ballX + ballDiam > screen_width)
velx = -velx;
if (ballY < 0)
vely = -vely;
if (ballY > screen_height)
{
width = 50;
height = 10;
myX = height/2 + 300;
myY = screen_height - height - 30;
// ball variables
ballX = 50;
ballY = 350;
ballDiam = 10;
velx = 2;
vely = -2;
for (int i = 0; i < BRICKS; i++)
{
bricks.alive = true;
}
}
if (checkCollision(ballX, ballY, ballDiam, ballDiam, myX, myY, width, height))
{
vely = -vely;
}
// Rendering //
Render();
SDL_Delay(1);
}
SDL_Quit();
return 0;
}
/********* Function Definitions *********/
void initGraphicStuff()
{
SDL_Init(SDL_INIT_EVERYTHING);
// Set OpenGL memory usage
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_WM_SetCaption( "Our first game", NULL );
SDL_SetVideoMode( screen_width, screen_height, 32, SDL_OPENGL );
glClearColor(1.0, 1.0, 1.0, 1.0);
glViewport( 0, 0, screen_width, screen_height );
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION); // 2D Rendering
glLoadIdentity(); // We "Save" it
glDisable(GL_DEPTH_TEST);
}
void Render()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); // Start phase
glOrtho(0, screen_width, screen_height, 0, -1, 1); // Set the matrix
glBegin(GL_QUADS);
glColor4f(0.0, 0.0, 0.0, 1.0); // Black
glVertex2f(myX, myY);
glVertex2f(myX + width, myY);
glVertex2f(myX + width, myY + height);
glVertex2f(myX, myY + height);
glEnd();
// BRICKS
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
for (int i = 0; i < BRICKS; i++)
{
if (bricks.alive == true)
{
if ( i % 2 == 0) glColor4f (0.6f, 2.0f, 0.6f, 1.0f);
else glColor4f (0.0f, 0.6f, 1.0f, 1.0f);
glVertex2f(bricks.x, bricks.y);
glVertex2f(bricks.x + bricks.width, bricks.y);
glVertex2f(bricks.x + bricks.width, bricks.y + bricks.height);
glVertex2f(bricks.x, bricks.y + bricks.height);
}
}
glEnd();
// BALL
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(ballX, ballY);
glVertex2f(ballX + ballDiam, ballY);
glVertex2f(ballX + ballDiam, ballY + ballDiam);
glVertex2f(ballX, ballY + ballDiam);
glEnd();
glPopMatrix(); // End phase
SDL_GL_SwapBuffers();
}
bool checkCollision(float Ax, float Ay, float Aw, float Ah, float Bx, float By, float Bw, float Bh)
{
if (Ay + Ah < By) return false;
else if (Ay > By + Bh) return false;
else if (Ax+Aw < Bx) return false;
else if (Ax > Bx + Bw) return false;
return true;
}
GLuint LoadTexture( const std::string &fileName)
{
SDL_Surface *image = IMG_Load(fileName.c_str() );
SDL_DisplayFormatAlpha(image);
unsigned object(0);
glGenTextures(1, &object);
glBindTexture(GL_TEXTURE_2D, object);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
SDL_FreeSurface(image);
return object;
}
SDL.lib; SDLmain.lib and SDL_image.lib are all included and directories are linked etc.
Really this makes absolutely no sense.