• Advertisement

Archived

This topic is now archived and is closed to further replies.

Problem with SDL...

This topic is 5897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After I read your tutorials about SDL I started to write my own SDL program. But I have a problem : when I compile my program (in a DOS screen) the screen is just black. Oh yeah, if i don''t use ''getch()'' the screen always goes away. What am I doing wrong ? here is my code (don''t mind my variable and function names, they are all in Dutch :-) ) //////////////////////////CODE///////////////////////////////// #include #include #include #include "SDL/SDL.h" SDL_Surface *Beeld; SDL_Surface *Bal; void InitBal() { Bal = SDL_LoadBMP("balletje.bmp"); } void TekenBal(SDL_Surface *img, int x, int y) { SDL_Rect dest; dest.x = x; dest.y = y; SDL_BlitSurface(img, NULL, Beeld, &dest); } int main(int argc, char *argv[]) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { printf("Unable to init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); Beeld = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF); InitBal(); TekenBal(Bal, 50,50); getch(); return 0; } ////////////////////////CODE/////////////////////////////////// Thanks in advance ! Bert.

Share this post


Link to post
Share on other sites
Advertisement
You have no real message loop. In order for the window to remain, you want to wait until a quit message has been sent. Here is an example:
  
// Look for our messages

while( SDL_WaitEvent( &event ) >= 0 )
{
// we got a message

// lets see what it is

switch( event.type ) {
// we place any case for any message we want to process

// the event filter function does work on our event before we

// process it here. Since this is a very simple program, all we

// handle is quit and we have no event filter

case SDL_QUIT:
{
// Close our program

exit( 0 );
}
break;
}
}


As you can see, without this, the program just runs and exits.

-----------------------------
kevin@mayday-anime.com
http://games.mayday-anime.com

Edited by - grasshopa55 on November 21, 2001 9:20:13 AM

Share this post


Link to post
Share on other sites
Here is a link to my example code

Click Me!

-----------------------------
kevin@mayday-anime.com
http://games.mayday-anime.com


Edited by - grasshopa55 on November 21, 2001 9:25:38 AM

Share this post


Link to post
Share on other sites
grasshopa55 is right. You need to have some loop-like thingy.
One REALLY easy way to do it is:

for(int i=0;i<10000;i++) TekenBal(Bal, 50,50);

You still would need to add some event handling stuff. Check out lesson 2 or 3 for some loops with event handling -n- stuff. Just replace DrawScene(...) with TekenBal(...) in these loops.



---
cone3d
http://cone3d.gamedev.net
Multitasking - screwing up several things at once.

Share this post


Link to post
Share on other sites
Does that .zip file you gave contain Windows code with SDL, or Linux? (Never used SDL and hoping it is Windows so I can figure it out, as I have been trying to do.)

Artificial intelligence is the devil... resist intelligent NPC''s


"He who fights with monsters should look to it that he himself does not become a monster... when you gaze long into the abyss, the abyss also gazes into you..."~Friedrich Nietzsche

Share this post


Link to post
Share on other sites
quote:
Original post by Drizzt DoUrden
Does that .zip file you gave contain Windows code with SDL, or Linux? (Never used SDL and hoping it is Windows so I can figure it out, as I have been trying to do.)

Artificial intelligence is the devil... resist intelligent NPC''s


"He who fights with monsters should look to it that he himself does not become a monster... when you gaze long into the abyss, the abyss also gazes into you..."~Friedrich Nietzsche


if you visit www.libsdl.org, you can find some basic apps to use as a starting point for your projects...



"And that''s the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer

Share this post


Link to post
Share on other sites
Does Linux use the same includes?

Does Linux have the same functions?

Does Linux use the same libraries?

Is Linux code written differently? aka the C++ code.


I don''t know what things change from Linux to Windows.

Artificial intelligence is the devil... resist intelligent NPC''s


"He who fights with monsters should look to it that he himself does not become a monster... when you gaze long into the abyss, the abyss also gazes into you..."~Friedrich Nietzsche

Share this post


Link to post
Share on other sites
When you use dev-c++ (with mingw32 (gcc for windows)) then all your code is (95% of the time) directly compilable in linux.
You only have different compile commands in linux and windows. You usually do not need to change the code. You must also have the same libraries (like SDL, ...) in windows and linux.


---
cone3d
http://cone3d.gamedev.net
Multitasking - screwing up several things at once.

Share this post


Link to post
Share on other sites
I use MSVC++6 and I keep getting tons of linking errors running your code. I downloaded the SDL libraries and headers off of www.libsdl.org for Windows, and I put them in the correct places. I also added sdl.lib to my project. I still get linking errors.

Share this post


Link to post
Share on other sites
You have to change the run-time library to "Multi-Threaded DLL"
Drizzt.

you''ll find it in Project->Settings->CodeGeneration
or something...
If that''s not it, find the file VisualC.html and read it through.

Share this post


Link to post
Share on other sites
Can't find "Multithreaded DLL" anywhere in MSVC++. Does anyone who has/uses MSVC++ know how to make Simple DirectMedia Layer work with it!?!?

Here is the the debug message:

--------------------Configuration: SDL Basic - Win32 Debug--------------------
Linking...
msvcrt.lib(MSVCRT.dll) : error LNK2005: _exit already defined in LIBCD.lib(crt0dat.obj)
msvcrt.lib(MSVCRT.dll) : error LNK2005: _strncpy already defined in LIBCD.lib(strncpy.obj)
msvcrt.lib(MSVCRT.dll) : error LNK2005: _fclose already defined in LIBCD.lib(fclose.obj)
msvcrt.lib(MSVCRT.dll) : error LNK2005: __isctype already defined in LIBCD.lib(isctype.obj)
LIBCD.lib(crt0init.obj) : warning LNK4098: defaultlib "msvcrt.lib" conflicts with use of other libs; use /NODEFAULTLIB:library
Debug/SDL Basic.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

SDL Basic.exe - 5 error(s), 1 warning(s)

    #include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#include <SDL.h>

void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
if ( SDL_LockSurface(screen) < 0 )
{
return;
}
}
}

void Sulock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
SDL_UnlockSurface(screen);
}
}

void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel)
{
case 1: // Assuming 8-bpp

{
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp

{
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used

{
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp

{
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
break;
}
}

void DrawScene(SDL_Surface *screen)
{
Slock(screen);
for(int x=0;x<640;x++)
{
for(int y=0;y<480;y++)
{
DrawPixel(screen, x,y,y/2,y/2,x/3);
}
}
Sulock(screen);
SDL_Flip(screen);
}

int main(int argc, char *argv[])
{

if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

SDL_Surface *screen;
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
int done=0;

while(done == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}

DrawScene(screen);
}

return 0;
}


Theres the code.

By the way, I didn't read the tutorial yet. I justr copied the code without a clue of what it does to see if I could get it to work. I am assuming that it creates a window but all I get is a pile of linking errors.

Edited by - Drizzt DoUrden on November 23, 2001 12:39:37 AM

Share this post


Link to post
Share on other sites
This is how I do it:
  • Create a new Win32 project
  • Goto Project->Settings->C/C++->Code Generation and set the "run-time library" to Multithreaded (I don''t know if that is required or not, actually)
  • Goto Project->Settings->Link->General and replace the "Object/library modules" textbox with: msvcrt.lib libc.lib libcp.lib kernel32.lib sdl.lib sdlmain.lib
  • Check "Ignore all default libraries"
  • Make sure to use "int main(int argc, char *argv[])" as your entry point not "int main(void)" or WinMain(...)

    [Resist Windows XP''s Invasive Production Activation Technology!]

    Share this post


    Link to post
    Share on other sites
    Thank you Null and Void. I did all of those things, and a few errors were resolved which I made me scream halleulah(also makes me wish I could spell it.) but I still get this:

    --------------------Configuration: SDL Basic - Win32 Debug--------------------
    Compiling...
    FirstSDL.cpp
    Linking...
    msvcrt.lib(bsku.obj) : error LNK2001: unresolved external symbol __imp__MessageBoxA@16
    Debug/SDL Basic.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    SDL Basic.exe - 2 error(s), 0 warning(s)

    Share this post


    Link to post
    Share on other sites
    I just followed my own steps using the code you posted, and it compiled fine. You aren''t calling MessageBox, are you? Remember that SDL is crossplatform, but MessageBox isn''t . You can use printf in SDL and it will go to a file called "stdout.txt" in the same directory as the program (in Windows, in Linux it goes to the console or no where ).

    Also, don''t even bother including windows.h unless you have to due to Microsoft''s OpenGL headers. But, if you do, do it within a ifdef/endif block for _WIN32.

    [Resist Windows XP''s Invasive Production Activation Technology!]

    Share this post


    Link to post
    Share on other sites
    I copied the code directly from cone3d.gamedev.net''s first tutorial. Their code is for Linux, and everyone keeps saying that there is no difference between Linux and Windows SDL, but I think they are leaving out the fact that C++ is there, too. I don''t know what changes from Linux to Windows from SDL or C++ or the operating system itself.

    I don''t see a call to MessageBox anywhere, but it is 1:20 AM here and my eyes are half shut so I might have missed it.

    BTW: I got rid of Windows.h alittle while ago. I noticed that there was no call to anything windows related.

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster
    If you stick to SDL, OpenGL and ANSI C/C++, your code should be 100% portable, as long as the libraries are present and than the compiler is ANSI (unlike MSVC).

    As for your problem, have you added SDL.lib to your project? I haven''t used MSVC (or Windows for that matter) in ages, but I recall than you had to manually add .lib files other than libc(d).lib and MS libs.

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by Drizzt DoUrden
    I don''t see a call to MessageBox anywhere, but it is 1:20 AM here and my eyes are half shut so I might have missed it.

    Yeah, that''s why the thoughtful dudes at MS put a find tool in the IDE, just for days like this when you''ve been up all night looking for an extraneous comma in a 1.5k line source file...

    Share this post


    Link to post
    Share on other sites
    No, there is no call to messagebox.

    But I am a genius.

    The problem was the libraries you told me to link, Null and Void. While they belonged there, too, so did a few other default ones that you told me to replace. I re-added them and it works like a charm!

    I needed all of these .libs:

    msvcrt.lib libc.lib libcp.lib sdl.lib sdlmain.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib

    Everything else was O.K. and there was no call to MessageBox anywhere.

    Thank you guys for your help!

    Share this post


    Link to post
    Share on other sites
    I apologize for not responding sooner, holiday weekend and all. The code I posted compiles fine for me, but linking is indeed an issue. The easiest way to get SDL to link using MSVC++ is to do the following.

    1. Go to Project->Settings->C++->Code Generations: Set Use Runtime Library to Debug Multithreaded DLL

    2. Go to Project->Settings->Link add SDL.lib and SDL main.lib to the exisiting Object/library modules path. Also, add msvcrt to the Ignore libraries path.

    This should get you up and running. If you have any questions, please email me at the address below. Thanks.


    -----------------------------
    kevin@mayday-anime.com
    http://games.mayday-anime.com

    Share this post


    Link to post
    Share on other sites
    Thanks. I got it working a few days ago, and I am on to tutorial two now. These tutorials are really good. I can''t wait to see more!

    ------------------------------
    (Since noone seems to look at the article proposal forums..)
    Interested in Flash tutorials on GameDev? Speak Up Here!

    "He who fights with monsters should look to it that he himself does not become a monster... when you gaze long into the abyss, the abyss also gazes into you..."~Friedrich Nietzsche

    Share this post


    Link to post
    Share on other sites

    • Advertisement