Skip to main content
gamedev.net
🔒 Locked

Problem with SDL...

Started by HolyFish Nov 21, 2001 at 6:45 AM 21 replies 3.5k views
Original Post
HolyFish
HolyFish
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.
grasshopa55
grasshopa55
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
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com
grasshopa55
grasshopa55
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
-----------------------------kevin@mayday-anime.comhttp://www.mayday-anime.com
cone3d
cone3d
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.
---cone3dhttp://cone3d.gamedev.netMy software never has any bugs - it just generates random features
Drizzt DoUrden
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
------------------------------Put THAT in your smoke and pipe it
Cyberdrek
Cyberdrek
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
[Cyberdrek | ]
Drizzt DoUrden
Drizzt DoUrden
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
------------------------------Put THAT in your smoke and pipe it
cone3d
cone3d
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.
---cone3dhttp://cone3d.gamedev.netMy software never has any bugs - it just generates random features
Drizzt DoUrden
Drizzt DoUrden
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.
------------------------------Put THAT in your smoke and pipe it
Lowas
Lowas
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.
C++, C++, C++
Drizzt DoUrden
Drizzt DoUrden
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
------------------------------Put THAT in your smoke and pipe it
Null and Void
Null and Void
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!]
  • hlusnagen
    hlusnagen
    SDL? multi-platform?

    Drizzt DoUrden
    Drizzt DoUrden
    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)
    ------------------------------Put THAT in your smoke and pipe it
    Null and Void
    Null and Void
    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!]
    Drizzt DoUrden
    Drizzt DoUrden
    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.
    ------------------------------Put THAT in your smoke and pipe it
    Oluseyi
    Oluseyi
    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...
    Drizzt DoUrden
    Drizzt DoUrden
    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!
    ------------------------------Put THAT in your smoke and pipe it

    Topic Locked

    This topic has been locked by a moderator. New replies are not allowed.

    Sign in to reply to this topic.