Problems creating a visual C++ project which links SDL

I downloaded SDL-1.2.2.zip and built the projects, which include the
SDL
libraries and some demos. It all builds without error and the demos I
have tried do work.

Following the instructions in the enclosed document “Using SDL with
Microsoft
Visual C++ 5 and 6” by Lion Kimbro I created a Visual C++ 6.0 project
to
compile the very simple SDL initialization example program in the SDL
documentation. I have been
VERY careful to follow the instructions on setting up the project, to
the letter. I’ve repeated the steps
a few times, gone over them for a couple of days, and the only way I
have been
able to get it to compile and link is with strange voodoo shit. It
goes like
this, here is the c program:

#include “SDL.h” /* All SDL App’s need this */
#include <stdio.h>

int main() {

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
    printf("Could not initialize SDL: %s.\n", SDL_GetError());
    exit(-1);
}

printf("SDL initialized.\n");

printf("Quiting SDL.\n");

/* Shutdown all subsystems */
SDL_Quit();

printf("Quiting....\n");

exit(0);

}

When I try to compile the project, I get this error message:___________________________________________________________________
--------------------Configuration: VideoTimerA - Win32
Debug--------------------
Compiling…
VideoTimerA.cpp
c:\newsource\videotimera\videotimera.cpp(11) : error C2065: ‘exit’ :
undeclared
identifier
Error executing cl.exe.

VideoTimerA.exe - 1 error(s), 0 warning(s)


Figuring that the problem might be caused by C vs C++ linking
issues, I
renamed by program file with a .c extension instead of a .cpp
extension, I get
this


-------------------Configuration: VideoTimerA - Win32
Debug--------------------

Compiling…
VideoTimerA.c
c:\newsource\videotimera\videotimera.c(4) : warning C4026: function
declared
with formal parameter list
c:\newsource\videotimera\videotimera.c(11) : warning C4013: 'exit’
undefined;
assuming extern returning int
c:\newsource\videotimera\videotimera.c(24) : warning C4716: ‘SDL_main’
: must
return a value
Linking…

VideoTimerA.exe - 0 error(s), 3 warning(s)


So I revert back to the original .cpp file extension, and it compiles
without
error:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------

VideoTimerA.exe - 0 error(s), 0 warning(s)


And it executes without error also.

So I though I had the problem fixed with magic, but then when I add
more C++ files to the project and my own header file I get the same
problems all over again.

What am I doing wrong ? This is making me crazy.

Could perhaps someone send me a really simple plain-vanilla VC++6
project which links SDL and does compile and link reliably, without
error ? Or of course if someone just knows the cause…

Best,

Allen

you know, I had a similar problem and it pissed the hell out of me. I
couldn’t fix it by any way other than copying someone else’s project file
and putting my files in instead. Attached is my project file, stripped
down. This works fine for me. I forget if you need the .dsw or .dsp
file…so I attached both.--------------------------------------------------------
Jeff W.
ICQ: #17989474


http://8bitavatar.org

----- Original Message -----
From: allen.ingling@nyu.edu (Allen W. Ingling)
To:
Sent: Thursday, October 11, 2001 7:25 PM
Subject: [SDL] problems creating a visual C++ project which links SDL

I downloaded SDL-1.2.2.zip and built the projects, which include the
SDL
libraries and some demos. It all builds without error and the demos I
have tried do work.

Following the instructions in the enclosed document “Using SDL with
Microsoft
Visual C++ 5 and 6” by Lion Kimbro I created a Visual C++ 6.0 project
to
compile the very simple SDL initialization example program in the SDL
documentation. I have been
VERY careful to follow the instructions on setting up the project, to
the letter. I’ve repeated the steps
a few times, gone over them for a couple of days, and the only way I
have been
able to get it to compile and link is with strange voodoo shit. It
goes like
this, here is the c program:

#include “SDL.h” /* All SDL App’s need this */
#include <stdio.h>

int main() {

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
    printf("Could not initialize SDL: %s.\n", SDL_GetError());
    exit(-1);
}

printf("SDL initialized.\n");

printf("Quiting SDL.\n");

/* Shutdown all subsystems */
SDL_Quit();

printf("Quiting....\n");

exit(0);

}

When I try to compile the project, I get this error message:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------
Compiling…
VideoTimerA.cpp
c:\newsource\videotimera\videotimera.cpp(11) : error C2065: ‘exit’ :
undeclared
identifier
Error executing cl.exe.

VideoTimerA.exe - 1 error(s), 0 warning(s)


Figuring that the problem might be caused by C vs C++ linking
issues, I
renamed by program file with a .c extension instead of a .cpp
extension, I get
this


-------------------Configuration: VideoTimerA - Win32
Debug--------------------

Compiling…
VideoTimerA.c
c:\newsource\videotimera\videotimera.c(4) : warning C4026: function
declared
with formal parameter list
c:\newsource\videotimera\videotimera.c(11) : warning C4013: ‘exit’
undefined;
assuming extern returning int
c:\newsource\videotimera\videotimera.c(24) : warning C4716: ‘SDL_main’
: must
return a value
Linking…

VideoTimerA.exe - 0 error(s), 3 warning(s)


So I revert back to the original .cpp file extension, and it compiles
without
error:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------

VideoTimerA.exe - 0 error(s), 0 warning(s)


And it executes without error also.

So I though I had the problem fixed with magic, but then when I add
more C++ files to the project and my own header file I get the same
problems all over again.

What am I doing wrong ? This is making me crazy.

Could perhaps someone send me a really simple plain-vanilla VC++6
project which links SDL and does compile and link reliably, without
error ? Or of course if someone just knows the cause…

Best,

Allen


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

-------------- next part --------------
A non-text attachment was scrubbed…
Name: NetworkedDeath.dsw
Type: application/octet-stream
Size: 621 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20011011/c9c2ac7e/attachment.obj
-------------- next part --------------
A non-text attachment was scrubbed…
Name: NetworkedDeath.dsp
Type: application/octet-stream
Size: 4017 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20011011/c9c2ac7e/attachment-0001.obj

Never mind, I figured it all out.

It was a combination of things. The files need to be compiled with C not

C++ style naming. The line in the instructions which reads "Create a C++

file for your project" might be more helpful if it was instead "Create a

C file for your project, not a C++ file". And VC++ has this feature where

if you’ve compiled a project with a C file it uses C style name
decoration, and if you switch to a C++ file it doesn’t change to using C++

name decoration unless you “clean” the project. And the warnings are
harmless. And the reason they went away when I switched to a C++ file
from C is that whenever you recompile without changes to the source,
VC++ doesn’t issue warnings.

“Allen W. Ingling” wrote:> I downloaded SDL-1.2.2.zip and built the projects, which include the

SDL
libraries and some demos. It all builds without error and the demos I
have tried do work.

Following the instructions in the enclosed document “Using SDL with
Microsoft
Visual C++ 5 and 6” by Lion Kimbro I created a Visual C++ 6.0 project
to
compile the very simple SDL initialization example program in the SDL
documentation. I have been
VERY careful to follow the instructions on setting up the project, to
the letter. I’ve repeated the steps
a few times, gone over them for a couple of days, and the only way I
have been
able to get it to compile and link is with strange voodoo shit. It
goes like
this, here is the c program:

#include “SDL.h” /* All SDL App’s need this */
#include <stdio.h>

int main() {

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
    printf("Could not initialize SDL: %s.\n", SDL_GetError());
    exit(-1);
}

printf("SDL initialized.\n");

printf("Quiting SDL.\n");

/* Shutdown all subsystems */
SDL_Quit();

printf("Quiting....\n");

exit(0);

}

When I try to compile the project, I get this error message:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------
Compiling…
VideoTimerA.cpp
c:\newsource\videotimera\videotimera.cpp(11) : error C2065: ‘exit’ :
undeclared
identifier
Error executing cl.exe.

VideoTimerA.exe - 1 error(s), 0 warning(s)


Figuring that the problem might be caused by C vs C++ linking
issues, I
renamed by program file with a .c extension instead of a .cpp
extension, I get
this


-------------------Configuration: VideoTimerA - Win32
Debug--------------------

Compiling…
VideoTimerA.c
c:\newsource\videotimera\videotimera.c(4) : warning C4026: function
declared
with formal parameter list
c:\newsource\videotimera\videotimera.c(11) : warning C4013: 'exit’
undefined;
assuming extern returning int
c:\newsource\videotimera\videotimera.c(24) : warning C4716: ‘SDL_main’
: must
return a value
Linking…

VideoTimerA.exe - 0 error(s), 3 warning(s)


So I revert back to the original .cpp file extension, and it compiles
without
error:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------

VideoTimerA.exe - 0 error(s), 0 warning(s)


And it executes without error also.

So I though I had the problem fixed with magic, but then when I add
more C++ files to the project and my own header file I get the same
problems all over again.

What am I doing wrong ? This is making me crazy.

Could perhaps someone send me a really simple plain-vanilla VC++6
project which links SDL and does compile and link reliably, without
error ? Or of course if someone just knows the cause…

Best,

Allen


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

This is just my two cents, but relying on the project file is not really a
good idea.

I have found that if you simply load up the C or CPP file, then hit compile,
VC will prompt you if you want a project created, for which you then answer
yes. Then you select multithreaded DLL in the compiler options and the
SDL_main and SDL.lib in the linker and it will usually work without any
additional link libraries (unless you need openGL in which you need to add
that too.) Add any additional C/CPP/H/HPP files and it should compile.

By default VC will create a console app (and STDERR/STDOUT will be displayed
in a console window.) But this can also be switched in the linker to
/subsystem:windows to make a win32 app without the console window.> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Allen W. Ingling
Sent: October 11, 2001 7:26 PM
To: sdl at libsdl.org
Subject: [SDL] problems creating a visual C++ project which links SDL

I downloaded SDL-1.2.2.zip and built the projects, which include the
SDL
libraries and some demos. It all builds without error and the demos I
have tried do work.

Following the instructions in the enclosed document “Using SDL with
Microsoft
Visual C++ 5 and 6” by Lion Kimbro I created a Visual C++ 6.0 project
to
compile the very simple SDL initialization example program in the SDL
documentation. I have been
VERY careful to follow the instructions on setting up the project, to
the letter. I’ve repeated the steps
a few times, gone over them for a couple of days, and the only way I
have been
able to get it to compile and link is with strange voodoo shit. It
goes like
this, here is the c program:

#include “SDL.h” /* All SDL App’s need this */
#include <stdio.h>

int main() {

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
    printf("Could not initialize SDL: %s.\n", SDL_GetError());
    exit(-1);
}

printf("SDL initialized.\n");

printf("Quiting SDL.\n");

/* Shutdown all subsystems */
SDL_Quit();

printf("Quiting....\n");

exit(0);

}

When I try to compile the project, I get this error message:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------
Compiling…
VideoTimerA.cpp
c:\newsource\videotimera\videotimera.cpp(11) : error C2065: ‘exit’ :
undeclared
identifier
Error executing cl.exe.

VideoTimerA.exe - 1 error(s), 0 warning(s)


Figuring that the problem might be caused by C vs C++ linking
issues, I
renamed by program file with a .c extension instead of a .cpp
extension, I get
this


-------------------Configuration: VideoTimerA - Win32
Debug--------------------

Compiling…
VideoTimerA.c
c:\newsource\videotimera\videotimera.c(4) : warning C4026: function
declared
with formal parameter list
c:\newsource\videotimera\videotimera.c(11) : warning C4013: 'exit’
undefined;
assuming extern returning int
c:\newsource\videotimera\videotimera.c(24) : warning C4716: ‘SDL_main’
: must
return a value
Linking…

VideoTimerA.exe - 0 error(s), 3 warning(s)


So I revert back to the original .cpp file extension, and it compiles
without
error:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------

VideoTimerA.exe - 0 error(s), 0 warning(s)


And it executes without error also.

So I though I had the problem fixed with magic, but then when I add
more C++ files to the project and my own header file I get the same
problems all over again.

What am I doing wrong ? This is making me crazy.

Could perhaps someone send me a really simple plain-vanilla VC++6
project which links SDL and does compile and link reliably, without
error ? Or of course if someone just knows the cause…

Best,

Allen


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

first of all, the exit function is defined in stdlib and not in stdio…

second, you should try to use the main function declared as "int main(int
argc, char **argv)“
instead of int main(void)”…

then, check if you have correctly configured the project workspace with the
right libraries,
then try again,

ps: you can keep the .c extension

good luck ;)> -----Message d’origine-----

De : sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org]De la part de
Allen W. Ingling
Envoye : vendredi 12 octobre 2001 03:26
A : sdl at libsdl.org
Objet : [SDL] problems creating a visual C++ project which links SDL

I downloaded SDL-1.2.2.zip and built the projects, which include the
SDL
libraries and some demos. It all builds without error and the demos I
have tried do work.

Following the instructions in the enclosed document “Using SDL with
Microsoft
Visual C++ 5 and 6” by Lion Kimbro I created a Visual C++ 6.0 project
to
compile the very simple SDL initialization example program in the SDL
documentation. I have been
VERY careful to follow the instructions on setting up the project, to
the letter. I’ve repeated the steps
a few times, gone over them for a couple of days, and the only way I
have been
able to get it to compile and link is with strange voodoo shit. It
goes like
this, here is the c program:

#include “SDL.h” /* All SDL App’s need this */
#include <stdio.h>

int main() {

printf("Initializing SDL.\n");

/* Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
    printf("Could not initialize SDL: %s.\n", SDL_GetError());
    exit(-1);
}

printf("SDL initialized.\n");

printf("Quiting SDL.\n");

/* Shutdown all subsystems */
SDL_Quit();

printf("Quiting....\n");

exit(0);

}

When I try to compile the project, I get this error message:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------
Compiling…
VideoTimerA.cpp
c:\newsource\videotimera\videotimera.cpp(11) : error C2065: ‘exit’ :
undeclared
identifier
Error executing cl.exe.

VideoTimerA.exe - 1 error(s), 0 warning(s)


Figuring that the problem might be caused by C vs C++ linking
issues, I
renamed by program file with a .c extension instead of a .cpp
extension, I get
this


-------------------Configuration: VideoTimerA - Win32
Debug--------------------

Compiling…
VideoTimerA.c
c:\newsource\videotimera\videotimera.c(4) : warning C4026: function
declared
with formal parameter list
c:\newsource\videotimera\videotimera.c(11) : warning C4013: 'exit’
undefined;
assuming extern returning int
c:\newsource\videotimera\videotimera.c(24) : warning C4716: ‘SDL_main’
: must
return a value
Linking…

VideoTimerA.exe - 0 error(s), 3 warning(s)


So I revert back to the original .cpp file extension, and it compiles
without
error:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------

VideoTimerA.exe - 0 error(s), 0 warning(s)


And it executes without error also.

So I though I had the problem fixed with magic, but then when I add
more C++ files to the project and my own header file I get the same
problems all over again.

What am I doing wrong ? This is making me crazy.

Could perhaps someone send me a really simple plain-vanilla VC++6
project which links SDL and does compile and link reliably, without
error ? Or of course if someone just knows the cause…

Best,

Allen


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I had a problem linking, when I used ‘int main()’. Changing it to ‘int
main(char **argv, int argc)’, fixed the problem. So check that out. If
anyone could explain it to me please…I have no idea why this should
fix the problem, but it does.

Allen W. Ingling wrote:>I downloaded SDL-1.2.2.zip and built the projects, which include the

SDL
libraries and some demos. It all builds without error and the demos I
have tried do work.

Following the instructions in the enclosed document “Using SDL with
Microsoft
Visual C++ 5 and 6” by Lion Kimbro I created a Visual C++ 6.0 project
to
compile the very simple SDL initialization example program in the SDL
documentation. I have been
VERY careful to follow the instructions on setting up the project, to
the letter. I’ve repeated the steps
a few times, gone over them for a couple of days, and the only way I
have been
able to get it to compile and link is with strange voodoo shit. It
goes like
this, here is the c program:

#include “SDL.h” /* All SDL App’s need this */
#include <stdio.h>

int main() {

printf(“Initializing SDL.\n”);

/* Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
printf(“Could not initialize SDL: %s.\n”, SDL_GetError());
exit(-1);
}

printf(“SDL initialized.\n”);

printf(“Quiting SDL.\n”);

/* Shutdown all subsystems */
SDL_Quit();

printf(“Quiting…\n”);

exit(0);
}

When I try to compile the project, I get this error message:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------
Compiling…
VideoTimerA.cpp
c:\newsource\videotimera\videotimera.cpp(11) : error C2065: ‘exit’ :
undeclared
identifier
Error executing cl.exe.

VideoTimerA.exe - 1 error(s), 0 warning(s)


Figuring that the problem might be caused by C vs C++ linking
issues, I
renamed by program file with a .c extension instead of a .cpp
extension, I get
this


-------------------Configuration: VideoTimerA - Win32
Debug--------------------

Compiling…
VideoTimerA.c
c:\newsource\videotimera\videotimera.c(4) : warning C4026: function
declared
with formal parameter list
c:\newsource\videotimera\videotimera.c(11) : warning C4013: 'exit’
undefined;
assuming extern returning int
c:\newsource\videotimera\videotimera.c(24) : warning C4716: ‘SDL_main’
: must
return a value
Linking…

VideoTimerA.exe - 0 error(s), 3 warning(s)


So I revert back to the original .cpp file extension, and it compiles
without
error:


--------------------Configuration: VideoTimerA - Win32
Debug--------------------

VideoTimerA.exe - 0 error(s), 0 warning(s)


And it executes without error also.

So I though I had the problem fixed with magic, but then when I add
more C++ files to the project and my own header file I get the same
problems all over again.

What am I doing wrong ? This is making me crazy.

Could perhaps someone send me a really simple plain-vanilla VC++6
project which links SDL and does compile and link reliably, without
error ? Or of course if someone just knows the cause…

Best,

Allen


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

it is in the SDL source code…

btw, it is not main(char**, int) but main(int, char**)…> -----Message d’origine-----

De : sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org]De la part de
Elik
Envoye : vendredi 12 octobre 2001 15:47
A : sdl at libsdl.org
Objet : Re: [SDL] problems creating a visual C++ project which links SDL

I had a problem linking, when I used ‘int main()’. Changing it to ‘int
main(char **argv, int argc)’, fixed the problem. So check that out. If
anyone could explain it to me please…I have no idea why this should
fix the problem, but it does.

Damien Mascre wrote:

it is in the SDL source code…

btw, it is not main(char**, int) but main(int, char**)…

oops.

and you still didn’t answer my question… why does this fix the problem?
is main() already declared in ‘sdl.h’, and we simply impelment it?>>

I had a problem linking, when I used ‘int main()’. Changing it to ‘int
main(char **argv, int argc)’, fixed the problem. So check that out. If
anyone could explain it to me please…I have no idea why this should
fix the problem, but it does.

Hello Elik,

Friday, October 12, 2001, 3:46:42 PM, you wrote:

E> I had a problem linking, when I used ‘int main()’. Changing it to ‘int
E> main(char **argv, int argc)’, fixed the problem. So check that out. If
E> anyone could explain it to me please…I have no idea why this should
E> fix the problem, but it does.

You seem to be under the impression that your main function is the
first function called when linking with SDL… Not so. In actual fact,
SDL calls the main function for you - and so you need to declare it
properly - which from memory is “int main(int argc, char **argv)”.

If you want to see how this works, use a debugger and continue tracing
after the end of your main() function. You’ll end up in the SDL code
whereas it would normally end.

Neil.

The answer is… linkage fails because int main() is slightly different
from int main(int, char **)… and why the linkage fails is because
the int main(int, char **) is explicitly called (not defined) somewhere
else… but the linker cant find it in your source files
let me explain…

windows gui applications entry point is WinMain… and this entry point
is hooked by the SDL lib (SDLmain.lib), and then, this hook
calls the main function (your’s) as defined in the SDLmain.h
header, at line 42 (SDL 1.2.1 source code):

/* The application’s main() function must be called with C linkage,
and should be declared like this:
#ifdef __cplusplus
extern “C”
#endif
int main(int argc, char *argv[])
{
}
*/
#define main SDL_main

/* The prototype for the application’s main() function */
extern C_LINKAGE int SDL_main(int argc, char *argv[]);

when you read that… it is clear that the WinMain hook calls
the SDL_main function, which is only an alias to your
’main’ function in your code. More elements can be found
in the src/main/win32/SDL_main.c file in the SDL branch to
show you how it is really implemented.

The main function that is called by the SDL hook is int main(int, char**)
and not int main(void), so the linker should be able to find it somewhere,
and the only place for it is your code.

You can also write a console based app, and using the same trick,
SDL hooks the ‘main’ (args dont matter) entry point function called
by the system and then call SDL_main, which is the same alias for the
’main’ function in your code…

you can continue this discussion on private mails if you want,
I prefer not pollute this mailing list with c programming basics.> -----Message d’origine-----

De : sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org]De la part de
Elik
Envoye : vendredi 12 octobre 2001 19:29
A : sdl at libsdl.org
Objet : Re: [SDL] problems creating a visual C++ project which links SDL

Damien Mascre wrote:

it is in the SDL source code…

btw, it is not main(char**, int) but main(int, char**)…

oops.

and you still didn’t answer my question… why does this fix the problem?
is main() already declared in ‘sdl.h’, and we simply impelment it?

I had a problem linking, when I used ‘int main()’. Changing it to ‘int
main(char **argv, int argc)’, fixed the problem. So check that out. If
anyone could explain it to me please…I have no idea why this should
fix the problem, but it does.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

-----Message d’origine-----
Damien Mascre
Envoye : vendredi 12 octobre 2001 21:04

you can continue this discussion on private mails if you want,
I prefer not pollute this mailing list with c programming basics.

see no harm in that, i am glad to help you, elik :slight_smile:

damien.> De : sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org]De la part de

A : sdl at libsdl.org
Objet : RE: [SDL] problems creating a visual C++ project which links SDL

hi,
i have this problem…whenever i try to pass a surface created with
SDL_CreateRGBSurface to glTexImage2D, i get a segfault…but images loaded
from file with IMG_Load works just fine…ummm… whats the probelm? am i
doin’ something wrong or SDL_CreateRGBSurface is buggy?

here’s the source included:
<------------------- sk_image.h ---------------->
#ifndef SK_IMAGE
#define SK_IMAGE

#include <GL/gl.h>
#include <SDL/SDL.h>
#include “String.h”

class sk_image
{
public:
sk_image() { };
sk_image(const SDL_Surface *img, const GLfloat x = 0.0f, const GLfloat y =
0.0f);
sk_image(const String filename, const GLfloat x = 0.0f, const GLfloat y =
0.0f);
~sk_image();

sk_image *crop(const int x, const int y, const int w, const int h) const;
void move(const GLfloat x, const GLfloat y) { _x = x; _y = y; };
void show() const;

private:
void _create(const SDL_Surface *img);
SDL_Surface *img;

GLuint  _texture;
GLfloat _x, _y;

};

#endif

<------------------- sk_image.h ---------------->

<------------------- sk_image.cc -------------->
#include
#include <SDL/SDL_image.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include “sk_image.h”

sk_image::sk_image(const String filename, const GLfloat x = 0.0f, const
GLfloat y = 0.0f)
{
img = IMG_Load(filename);
if(!img)
throw runtime_error ("Image load " + filename + “failed!..exiting, sorry”);

_x = x;
_y = y;
_create(img);

// SDL_FreeSurface(img);
}

sk_image::sk_image(const SDL_Surface *img, const GLfloat x = 0.0f, const
GLfloat y = 0.0f)
{
_x = x;
_y = y;
_create(img);
}

void sk_image::_create(const SDL_Surface *img)
{
glGenTextures(1, &_texture);
glBindTexture(GL_TEXTURE_2D, _texture);

cout << "Debug"  << img->w << ":" << *(long *)img->pixels << endl;
glTexImage2D(GL_TEXTURE_2D, 0, img->format->BytesPerPixel, img->w, img->h, 

0, GL_BGR, GL_UNSIGNED_BYTE, img->pixels);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

}

sk_image::~sk_image()
{
glDeleteTextures(1, &_texture);
}

sk_image *sk_image::crop(const int x, const int y, const int w, const int h)
const
{
sk_image *result = new sk_image();
int bpp = img->format->BytesPerPixel;

result->img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
							bpp,
							img->format->Rmask,
							img->format->Gmask,
							img->format->Bmask,
							img->format->Amask);

// result->img = img;
for(int i=0; i<h; ++i)
	memcpy((char *)result->img->pixels+i*w*bpp, (char *)img->pixels+i*w*bpp, 

w*bpp);

result->_create(result->img);
result->_x = 0.0f;
result->_y = 0.0f;

return result;

}

void sk_image::show() const
{
glPushMatrix();

glTranslatef(_x, _y, 0);
// glColor4f(1.0, 1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, _texture);

glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f,  1.0f);    

// Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
// Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Top Left Of The Texture and Quad
glEnd();

glPopMatrix();

}

<------------------- sk_image.cc -------------->

Create an RGBA surface, OpenGL textures use 32bit floating point RGBA
anyways.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, surface->w, surface->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, surface->pixels);

OR

Pass the texture as a RGB, not RGBA texture to OpenGL.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, surface->w, surface->h, 0, GL_RGB,
GL_UNSIGNED_BYTE, surface->pixels);

SDL surfaces are RGB/RGBA, so using BGR for the texture isn’t correct.

What you have:

glTexImage2D(GL_TEXTURE_2D, 0, img->format->BytesPerPixel, img->w, img->h,
0, GL_BGR, GL_UNSIGNED_BYTE, img->pixels);

In theory, if your IMG is 16bit, 8bit, or the width and/or height isn’t a
multiple of 2, you will also get a crash/segfault. The problem may be that
the pixel data isn’t BGR. (IE, the image you loaded isn’t what OpenGL
expected.)

hi,
thanks for your answer. it helped a lot.
cyaOn Saturday 13 October 2001 00:43, you wrote:

Create an RGBA surface, OpenGL textures use 32bit floating point RGBA
anyways.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, surface->w, surface->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);

OR

Pass the texture as a RGB, not RGBA texture to OpenGL.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, surface->w, surface->h, 0, GL_RGB,
GL_UNSIGNED_BYTE, surface->pixels);

SDL surfaces are RGB/RGBA, so using BGR for the texture isn’t correct.

What you have:

glTexImage2D(GL_TEXTURE_2D, 0, img->format->BytesPerPixel, img->w, img->h,
0, GL_BGR, GL_UNSIGNED_BYTE, img->pixels);

In theory, if your IMG is 16bit, 8bit, or the width and/or height isn’t a
multiple of 2, you will also get a crash/segfault. The problem may be that
the pixel data isn’t BGR. (IE, the image you loaded isn’t what OpenGL
expected.)

skinncode wrote:

i have this problem…whenever i try to pass a surface created with
SDL_CreateRGBSurface to glTexImage2D, i get a segfault…but images loaded
from file with IMG_Load works just fine…ummm… whats the probelm? am i
doin’ something wrong or SDL_CreateRGBSurface is buggy?

your problem is mismatching pixel formats. glTexImage2D expects the pixels
in one format (which one depends on how you call it; usually with the
bytes in R,G,B or R,G,B,A order. IMG_Load() may return an image in any
format; you cannot expect any format in particular to come out from it.
But when calling SDL_CreateRGBSurface() you have total control of the
pixel format; be careful when using it.

For some simple code that converts a loaded surface to RGBA or RGB format,
see ftp://ptah.lnf.kth.se/pub/misc/convgltex.c . Make sure you understand
how it works