• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Jossos

SDL IMG_load function crashing the program

4 posts in this topic

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

And I get this:

[img]http://img818.imageshack.us/img818/355/errorerror.jpg[/img]

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)

[CODE]
/*

*/
/********* 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;
}
[/CODE]

SDL.lib; SDLmain.lib and SDL_image.lib are all included and directories are linked etc.

Really this makes absolutely no sense.
0

Share this post


Link to post
Share on other sites
It appears you haven't initialized SDL_Image with [url=http://www.libsdl.org/projects/SDL_image/docs/SDL_image_8.html]IMG_Init[/url], so it would only be natural that IMG_LoadImage would fail. Be sure to check [url=http://www.libsdl.org/projects/SDL_image/docs/SDL_image_45.html]IMG_GetError[/url] and [url=http://sdl.beuc.net/sdl.wiki/SDL_GetError]SDL_GetError[/url] 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
0

Share this post


Link to post
Share on other sites
[quote name='fastcall22' timestamp='1356029370' post='5012878']
It appears you haven't initialized SDL_Image with [url="http://www.libsdl.org/projects/SDL_image/docs/SDL_image_8.html"]IMG_Init[/url], so it would only be natural that IMG_LoadImage would fail. Be sure to check [url="http://www.libsdl.org/projects/SDL_image/docs/SDL_image_45.html"]IMG_GetError[/url] and [url="http://sdl.beuc.net/sdl.wiki/SDL_GetError"]SDL_GetError[/url] 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.)
[/quote]

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
0

Share this post


Link to post
Share on other sites
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 [u]tell[/u] you what's wrong. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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.

[code]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;
}[/code]

Also, the error message that Visual Studios is popping up gives us some real clues to the problem. "[i]Access violation when reading 0x00000000[/i]"
0x00000000 is '0'. '0' is NULL. Visual Studio is [i]telling[/i] 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? [url="http://www.libsdl.org/projects/docs/SDL_image/SDL_image_7.html"]The documentation says 'yes'[/url].

Under what circumstances does it return NULL? "[i]NULL is returned on errors, such as no support built for the image, or a file reading error.[/i]"
Hmm, '[i]no support built for the image[/i]'? You initialized support for PNGs, but is the proper [b]libpng[/b] 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 ([url="http://www.libsdl.org/projects/SDL_image/docs/SDL_image_8.html"]see here[/url]) 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. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

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 [b]assert[/b](). [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

[hr]

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 [url="http://lazyfoo.net/SDL_tutorials/index.php"]start here[/url] before you toss more APIs (like OpenGL) into the mix.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0