Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


SDL IMG_load function crashing the program


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Jossos   Members   -  Reputation: 216

Like
0Likes
Like

Posted 20 December 2012 - 06:25 AM

So I'm following a tutorial and have the exact code of the guy in this vid: http://www.youtube.com/watch?annotation_id=annotation_518184&feature=iv&src_vid=UBxxVFjYN3c&v=bBm0pGwiyYE (he runs the code just fine).

And I get this:

Posted Image

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[i].x = x;
  bricks[i].y = y;
  bricks[i].width = 60;
  bricks[i].height = 20;
  bricks[i].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[i].alive == true)
   {
	if ( checkCollision(ballX, ballY, ballDiam, ballDiam, bricks[i].x, bricks[i].y, bricks[i].width, bricks[i].height) )
	{
	 if (velx > 0)
	  velx += 0.2;
	 else
	  velx -= 0.2;
	 if (vely > 0)
	  vely += 0.2;
	 else
	  vely -= 0.2;
	 velx = -velx;
	 bricks[i].alive = false;
	 break;
	}
   }
  }
  ballY += vely;
  for (int i = 0; i < BRICKS; i++)
  {
   if (bricks[i].alive == true)
   {
	if ( checkCollision(ballX, ballY, ballDiam, ballDiam, bricks[i].x, bricks[i].y, bricks[i].width, bricks[i].height) )
	{
	 if (velx > 0)
	  velx += 0.2;
	 else
	  velx -= 0.2;
	 if (vely > 0)
	  vely += 0.2;
	 else
	  vely -= 0.2;
	 vely = -vely;
	 bricks[i].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[i].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[i].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[i].x, bricks[i].y);
   glVertex2f(bricks[i].x + bricks[i].width, bricks[i].y);
   glVertex2f(bricks[i].x  + bricks[i].width, bricks[i].y + bricks[i].height);
   glVertex2f(bricks[i].x, bricks[i].y + bricks[i].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.

Sponsor:

#2 fastcall22   Crossbones+   -  Reputation: 4386

Like
0Likes
Like

Posted 20 December 2012 - 12:49 PM

It appears you haven't initialized SDL_Image with IMG_Init, so it would only be natural that IMG_LoadImage would fail. Be sure to check IMG_GetError and SDL_GetError to get extended error information if an operation (for the respective library) were to fail.

Also, if you are using Visual Studio, take note that the working directory of your executable is your project directory when running inside the Visual Studio, and the executable directory when running outside of the Visual Studio. (This can be changed in the debugging section of the project properties.)

Edited by fastcall22, 20 December 2012 - 12:56 PM.

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#3 Jossos   Members   -  Reputation: 216

Like
0Likes
Like

Posted 20 December 2012 - 01:50 PM

It appears you haven't initialized SDL_Image with IMG_Init, so it would only be natural that IMG_LoadImage would fail. Be sure to check IMG_GetError and SDL_GetError to get extended error information if an operation (for the respective library) were to fail.

Also, if you are using Visual Studio, take note that the working directory of your executable is your project directory when running inside the Visual Studio, and the executable directory when running outside of the Visual Studio. (This can be changed in the debugging section of the project properties.)


Yeah so I used IMG_Init(IMG_INIT_PNG); and it's still crashing. I understand how it works with the directories and such. Just to make sure, I have pasted the paddle.png file in every goddam folder in my project.

Seriously need help with this -___-

Edit: I used the error checking and got this: "Failed loading libpng15-15.dll: %1 is not a valid Win32 application."
libpng15-15.dll is in my project directory.

Textures are the bane of my existence

Edited by Jossos, 20 December 2012 - 02:08 PM.


#4 Jossos   Members   -  Reputation: 216

Like
1Likes
Like

Posted 20 December 2012 - 02:25 PM

Alright, so apparently I had to paste zlib1.dll in my project. Works fine now

#5 Servant of the Lord   Crossbones+   -  Reputation: 20348

Like
1Likes
Like

Posted 20 December 2012 - 02:38 PM

Instead of guessing what's wrong (by posting the image in every folder), the proper programmer thing to do would be to add some error-checking to your code (which you should add even when your code is working fine), to have your code tell you what's wrong. Posted Image

In this situation, you never check the result of IMG_Load() to make sure it's valid. Also, if it's invalid, you never ask SDL_image to tell you why it failed.

SDL_Surface *image = IMG_Load(fileName.c_str());
if(!image)
{
     std::cout << "LoadTexture() failed: Failed to load the image '" << fileName << "'.\nIMG_Load() : << IMG_GetError() << std::endl;
     return;
}

Also, the error message that Visual Studios is popping up gives us some real clues to the problem. "Access violation when reading 0x00000000"
0x00000000 is '0'. '0' is NULL. Visual Studio is telling you that you're trying to read from a pointer somewhere that is set to NULL (Again: code that tells you what's wrong is a wonderful wonderful thing).

So what pointers are you using? SDL_Surface*. Where do you get that pointer from? IMG_Load().
Does IMG_Load() return NULL pointers when it fails? The documentation says 'yes'.

Under what circumstances does it return NULL? "NULL is returned on errors, such as no support built for the image, or a file reading error."
Hmm, 'no support built for the image'? You initialized support for PNGs, but is the proper libpng DLL (which is probably runtime loaded by SDL_image) visible from your executable?

If not, you tell SDL_image to initialize (which, like fastcall22, I don't see anywhere in your code - are you sure you didn't forget it?), but the initialization fails, but you never know the initialization failed because you didn't check the result (see here) to see whether it succeeded or not. You then later call IMG_Load(), but it fails because the previous initialization failed, and again, no error checking catches it, and you then pass a NULL SDL_Surface* pointer to OpenGL which crashes worse than the five armies at the lone mountain.

This is just an educated guess, however. It might be completely off. Posted Image

IMG_Init() didn't return the result you expected (which is your code's responsibility to confirm).
IMG_Load() tells you it failed (but's your code's responsibility to check for it, by checking the returned surface).
IMG_Load() tells you why it failed with IMG_GetError() (but it's your code's responsibility to display the message to you as the user/developer).

OpenGL then crashes (because your code gave it an invalid pointer).
Visual Studio then tells you that your program crashed, why it crashed, and displays it to you in a pop-up message that can't be minimized (which is your responsibility as the developer to read).


For your coding sanity as a programmer, it is a very very important habit to error check your code; or at the very least, use assert(). Posted Image

 

By the way, you are trying to mix OpenGL and SDL together. Have you ever learned SDL on its own? If not, I suggest you start here before you toss more APIs (like OpenGL) into the mix.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS