GFX with SDL
Lesson 1: Getting started with SDL
by Marius Andra
Welcome to the first tut in the "GFX with SDL" series.
Using SDL with Dev-C++
The first thing you need is to download some files that you will need. They are in the zip
sdlDevCPP-1.2.4.zip (click the name to download). Extract the zip INSIDE your Dev-C++ directory so that the files from the folders lib and include
(from the zip) get extracted to the folders lib and include in your Dev-C++ directory. On my computer the Dec-C++
folder is c:\Dev-C++. So after extracting the zip I would have the files libSDL.a, libSDL.la, libSDLmain.a and
SDL.dll inside c:\Dev-C++\lib and lots of .h files inside c:\Dev-C++\include\SDL.
Now in Dev-C++ start a new console project. Now go to the project options dialog (found in the menu Project).
The thing we need to set here is the field that says "Further object files or linker options:". Type in
"-lmingw32 -lSDLmain -lSDL" (w/o the quotes) inside it. Now click OK.
One last thing: when you do printf(...) when having the SDL stuff in the project options in Dev-C++
the output of printf is written to a file called stdout.txt, not the screen.
And that's all the Dev-C++ related stuff.
Using SDL in Microsoft Visual C++ 6.0
To use SDL in MSVC6, download the file
SDL-devel-1.2.4-VC6.zip (if a newer version of sdl is released, look in www.libsdl.org for a newer version of this file).
In that file you'll find 2 important folders (amongst the others) - include and lib.
Extract all the contents from the lib folder in the zip into your MSVC6 lib folder. On my system it's:
C:\Program Files\Microsoft Visual Studio\VC98\Lib. Now create a new folder called SDL in your MSVC6 include folder and copy all the .h
files from the include folder in the zip file into the newly created folder (on my system it would be
C:\Program Files\Microsoft Visual Studio\VC98\Include\SDL) AND into the include folder itself (C:\Program Files\Microsoft Visual Studio\VC98\Include on my system).
Now in Visual C++, create a new project. Choose 'WIN32 Application' and, when prompted, select 'an empty project'.
Now you need to create some .cpp file for your project. Click file->new and select 'c++ source file'. Name it 'main.cpp'.
Now go to the project settings (from the menu: project->settings). Click the 'LINK' tab and add 'sdl.lib sdlmain.lib' to the end of the long line of other listed .lib files (Object/library modules).
One last thing: click the 'C/C++' tab in 'Project Settings'. From the drop-down menu choose 'Code Generation'. Now select 'Multithreaded DLL' from the 'Use run-time library' drop-down menu.
And you're basically all set with MSVC6. Now just code :).
SDL.dll
One important file with SDL is the file SDL.dll (it's included with the Dev-C++ and MSVC6 SDL zips (look above...)). If you would want to run SDL programs, then you MUST have the file
SDL.dll inside c:\windows\system (win 95, 98, ME) or c:\windows\system32 (on windows NT, 2000 and XP). OR you may have the file
SDL.dll in the SAME folder as the executable of your program. If you would want to distirbute your SDL programs
among friends then you MUST also give them the file SDL.dll. SO copy the file SDL.dll from c:\Dev-C++\lib into
c:\windows\system or c:\windows\system32 (on NT, 2000 and XP) on your system and distirbute it with your executables.
Getting started with SDL
You are basically all set. You only need to add the include file SDL/SDL.h to the top of your program like this:
#include <SDL/SDL.h>
Initalizing SDL is done through the SDL_Init() function. SDL_Init returns less than 0 on failure. It takes one
parameter: what to initalize. To initalize the video screen pass to it the constant SDL_INIT_VIDEO. To initalize
the audio, pass to it the constant SDL_INIT_AUDIO. To initalize the video and audio, pass to it
SDL_INIT_VIDEO|SDL_INIT_AUDIO. There are many more things that you can pass (seperating them with |'s when passing many at once). Here are the things you can pass:
SDL_INIT_TIMER
SDL_INIT_AUDIO
SDL_INIT_VIDEO
SDL_INIT_CDROM
SDL_INIT_JOYSTICK
SDL_INIT_NOPARACHUTE
SDL_INIT_EVENTTHREAD
SDL_INIT_EVERYTHING
So in conclusion if we would want to init the video and the audio we get:
if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) <0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
If an error occured, then the function SDL_GetError() would return a string about the error.
When exiting your C++ program, you must execute the function SDL_Quit(). That will clear up everything. If you
wouldn't execute it before exiting your program, starge things may occur. To tell the compiler that you want to run SDL_Quit on exit, tell it like
this:
atexit(SDL_Quit);
That way you don't need to put SDL_Quit() before every return [nr]; in main().
In SDL you have many surfaces. Everything is a surface. You can draw on a surface and you can also draw a
surface on an other surface. In SDL the screen is also a surface. A surface in our program is actually a pointer
to the structure SDL_Surface. To get the screen surface do this:
SDL_Surface *screen;
I'm sure you have seen at some point in your life that some game asked you for a screen resolution to run at.
If not, well then PLAY MORE GAMES. If you would want to use the surface screen (remember, screen is just the
name of a pointer to the structure SDL_Surface) as the surface you can draw on (and you will see what you have
drawn on your monitor) then use the function SDL_SetVideoMode();
screen = SDL_SetVideoMode(640, 480, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF);
The first three parameters are the width, height and bits per pixel of the screen. If you type in 0 for the BPP
then SDL would automatically select the best available BPP. The fourth parameter is used to give some special
flags. You must (almost) always give it SDL_HWSURFACE (or SDL_SWSURFACE) if you want a screen to draw on.
Here's a list of what you may give it:
SDL_SWSURFACE - Create the video surface in system memory
SDL_HWSURFACE - Create the video surface in video memory
SDL_ASYNCBLIT - Enables the use of asynchronous updates of the display surface. This will usually slow down blitting on single CPU machines, but may provide a speed increase on SMP systems.
SDL_ANYFORMAT - Normally, if a video surface of the requested bits-per-pixel (bpp) is not available, SDL will emulate one with a shadow surface. Passing SDL_ANYFORMAT prevents this and causes SDL to use the video surface, regardless of its pixel depth.
SDL_HWPALETTE - Give SDL exclusive palette access. Without this flag you may not always get the the colors you request with SDL_SetColors or SDL_SetPalette.
SDL_DOUBLEBUF - Enable hardware double buffering; only valid with SDL_HWSURFACE. Calling SDL_Flip will flip the buffers and update the screen. All drawing will take place on the surface that is not displayed at the moment. If double buffering could not be enabled then SDL_Flip will just perform a SDL_UpdateRect on the entire screen.
SDL_FULLSCREEN - SDL will attempt to use a fullscreen mode. If a hardware resolution change is not possible (for whatever reason), the next higher resolution will be used and the display window centered on a black background.
SDL_OPENGL - Create an OpenGL rendering context. You should have previously set OpenGL video attributes with SDL_GL_SetAttribute.
SDL_OPENGLBLIT - Create an OpenGL rendering context, like above, but allow normal blitting operations. The screen (2D) surface may have an alpha channel, and SDL_UpdateRects must be used for updating changes to the screen surface.
SDL_RESIZABLE - Create a resizable window. When the window is resized by the user a SDL_VIDEORESIZE event is generated and SDL_SetVideoMode can be called again with the new size.
SDL_NOFRAME - If possible, SDL_NOFRAME causes SDL to create a window with no title bar or frame decoration. Fullscreen modes automatically have this flag set.
My recommendation: give it SDL_HWSURFACE|SDL_DOUBLEBUF and in case of an error try again with SDL_SWSURFACE.
SDL_SetVideoMode returns a pointer to SDL_Surface if sucessful or NULL if not. To check for errors use this block
of code:
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
And that's all about initalizing SDL. You can now start drawing. But before we get into drawing stuff I'll tell
you about some new datatypes that SDL gave us so you don't get confused when you run into them. They are:
Uint8 - the equilant of an unsigned char
Uint16 - a 16 bit (2 byte) unsigned integer
Uint32 - a 32 bit (4 byte) unsigned integer
Uint64 - a 64 bit (8 byte) unsigned integer
Sint8 - the equilant of a signed char
Sint16 - a 16 bit (2 byte) signed integer
Sint32 - a 32 bit (4 byte) signed integer
Sint64 - a 64 bit (8 byte) signed integer
And one more thing: Sometimes when you get errors on initalizing stuff you don't need to exit completly.
For example when initalizing SDL_INIT_VIDEO passed and SDL_INIT_AUDIO did not, you can still continue with the
program, only without audio. To check (for example) if the audio initalization suceeded, use the SDL_WasInit()
function. Here's some code:
Uint32 init = SDL_WasInit(SDL_INIT_AUDIO);
if(init & SDL_INIT_AUDIO)
{
sound = 1; // Audio init sucessful, use sound
} else {
sound = 0; // Audio init unsucessful, don't use sound
}
You should add the code somewhere between the
if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) <0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
code in your own programs (I won't add it in the tuts (for now) for simplicity).
Drawing pixels isn't very easy at first, but when you have a function that does it for you it can't get any
easier. The function that I use for drawing pixels is taken from the SDL intro (found on www.libsdl.org).
It looks like this:
NOTE: you don't need to understand all of it. Just know that is works
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;
}
}
You pass it the surface you want to draw on, the x and y of the point and the RGB of the color you want to draw
the point in.
The following paragraph is a crash course in RGB. Don't read it if you don't want to.
Look at your monitor very closely with a magnifying glass (note: you may hurt your eyes if you do it a lot).
Look at some white part of the screen. You will see many dots: red, green and blue. Now look at some other part of
the screen (in some other color). You will see that the red, green and blue are not brightly lit, but have different
tones. With different RGB values you can display all the colours there are.
We will also need 2 more functions. With some computers (videocards) you need to lock a screen before you can draw
on it. SDL_MUSTLOCK(SDL_Surface *screen) tells us if we need to lock the screen and
SDL_LockSurface(SDL_Surface *screen) and SDL_UnlockSurface(SDL_Surface *screen) do the locking and unlocking.
Here are 2 functions that make it all easier:
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);
}
}
Just call Slock(screen) to lock the screen and Sulock(screen) to unlock it.
By now you should have something like this:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
// The functions are not shown to save space
void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B);
void Slock(SDL_Surface *screen);
void Sulock(SDL_Surface *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);
}
// DRAWING GOES HERE
return 0;
}
Now run it! You should get a blank window that comes and goes quickly. Add SDL_FULLSCREEN to the flags list
to see the screen turn black for some time. Now let's get drawing!
We will use a simple method when drawing everything: we draw everything into a buffer and when everything is
drawn we draw the buffer on the screen. This will be much useful than drawing each pixel on the screen and
updating the screen then. This method is faster and there won't be any flickering.
We want to make the entire screen colorful (like you saw on the screenshot). For that we loop through all the
x, y coordinates and draw a colorful pixel there. Before the loop we add the screen locking function and after
it we add the unlocking function. Drawpixel draws a colorful pixel (changes the color of a pixel) inside the screen surface (the buffer) and
we use SDL_Flip to draw the buffer (screen surface) on the actual computer 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);
NOTE: Actually drawing everything on the screen all the time can be quite slow at times. Usually when you only
need to draw one part of the screen you only draw one part of the screen. More in a future lesson.
Replace the // DRAWING GOES HERE with the drawing code block and run the program. You should see the black window
as a colorful window now. But still it only stays for a very little time. To make the colorful window stay longer
put the drawing code inside a for loop:
for(i=0;i<100;i++)
{
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);
}
That will draw the same block of code 100 times and then exit. But still there is a better way.
We put the entire drawing code inside a new function:
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);
}
And now in main() we start a thing called a game loop. A game loop is a loop that runs until it is exited.
With each run of the loop some stuff occur. Our game loop is a while loop that runs as long as a previously
defined integer done equals 0.
int done=0;
while(done == 0)
{
// CODE
}
Inside our game loop we check if ESCAPE or if the X button on the window has been pressed. If so we make done
equal 1 and the loop just won't run next time.
All SDL event thingys are in the structure SDL_Event. We need to have one instance of it to check for events.
SDL_Event event;
We then poll for events (until there are no more to poll).
while ( SDL_PollEvent(&event) )
{
}
With each while(...) {...} the SDL_Event structure will contain some info about some events. We then check for the
event type (inside the SDL_PollEvent while loop)
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
// CODE
}
If we get the quit event (the close button on the window being pressed) then we make done equal 1.
If we get a key down event then we must also check which key got pressed:
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
All key names start with SDLK_. Check the file SDL_keysym.h for more SDLK_ key names.
After the event checking we call:
DrawScene(screen);
And that's it! Here's the full source code:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/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;
}
Bramnice tut, good basix :)
grapgot some problems with the first 'working' example:
when i'm trying to compile the code with my dev-cpp 4.9.4.1 and SDL, i got an error message like this:
[warning] In function 'SDL_main':
parse error before '*'
'screen' undeclared (first use this function)
(Each un.. blah blah)
grap:D maybe it's easier if i first put the 'c++' option on :D
Larryanyone want to tell me how i can get different colors
NightmareCoderwow, really great tutorial. Good work!
Motagaly/where is the code of all the 6 tutorials
edwow a nice tut tho the language sucks partially i mean spelling etc. 'initally' wtf hurts my eyes well mby thats coz im not a native speaker so ive had to learn the precise way in which to spell wrds
mariused, I'm not a native english speaker myself. I know my english isn't the best. I should have run this and the other tutorials through a spell checker, but since I didn't have one (didn't bother to find one) I didn't :(. If you want better spelling in the tuts, then read the PDF tutorials, they were spell checked before being converted to PDFs by Devin. And know this: <b>EVERYONE MAKES TYPOS!</B>
mariusLarry, modify this line a bit:
DrawPixel(screen, x,y,y/2,y/2,x/3);
try out different things with the last 3 parameters, for example:
DrawPixel(screen, x,y,x/3,y/2,x/3);
DrawPixel(screen, x,y,x/3,x/3,y/2);
etc
mariuslook up <a href="http://www.google.com/">linear interpolation</a> if you want to know the correct way to fade from one color to an other.
maestinis there anywhere I can read more details about how the pixel-plotting function works
XterriaI'm feeling the urge
c0d3fuYay - no more winmain()! DirectX pales b4 the might of SDL.
icewindworx great w/ vc++6 too. nice. thx^_^
darkcoder8In reference to the DrawPixel(screen, x,y,y/2,y/2,x/3); bit,
can i change the colors of the pixels only and not the shapes they make and visa versa
CoDeRafter following instructions...
Dev-C++ still tells me:
[Warning] In function console_main
[Linker Error] undefing reference to 'SDL_SetModulHandle'
[Linker Error] undefing reference to 'SDL_main'
anyone can help
CooljerkI'm running Visual C++ 5, and I get the following error while trying to Build begining.exe (Begining being the name of the project)
--------------------Configuration: Begining - Win32 Debug--------------------
Linking...
D:\Program Files\DevStudio\VC\LIB\sdl.lib : fatal error LNK1106: invalid file or disk full: cannot seek to 0x3cb918da
Error executing link.exe.
Begining.exe - 1 error(s), 0 warning(s)
What is that
CooljerkI'm running Visual C++ 5, and I get the following error while trying to Build begining.exe (Begining being the name of the project)
--------------------Configuration: Begining - Win32 Debug--------------------
Linking...
D:\Program Files\DevStudio\VC\LIB\sdl.lib : fatal error LNK1106: invalid file or disk full: cannot seek to 0x3cb918da
Error executing link.exe.
Begining.exe - 1 error(s), 0 warning(s)
What is that
fryhey there.
great tutorial! helps me heaps!
i havent been able to do anything in Dev C++ graphics until now :)
SiliconSnailwow. I've always had trouble with programing graphics, but this tutorial finaly sheds some light on me! Awsome job!
huesoGreat tut. In your "void Slock(SDL_Surface *screen)"
function, watch out. it returns a value, so the return type must be int.
kanrealy nice tutorial, keep up the good work :)
KettilCoDeR...
Did you remember to write: -lminqw32 -lSDLmain -lSDL
In the linker options field in the project options
KettilKettil(I think you're suposed to do that every time you start a new project..)ohh... you can't use questionmark....
KettilGreat tutorial by the way!!!!!!
MuteInverti'm using this code:
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int SDL_INIT(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
if (SDL_INIT(SDL_INIT_VIDEO|SDL_INIT_AUDIO) <0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
}
and when i compile it, it says(in the linker):
C:\DEV-C_~1\LIB\\libSDLmain.a(SDL_main.o): In function `console_main':
/home/hercules/release/SDL-1.2.4/src/main/SDL_main.c:227: undefined reference to `SDL_main'
i'm very sure i installed all the sdl components perfectly, so why doesn't it work
DavidJust a little note:
http://www.bloodshed.net/dev/packages/sdl.html
contains a package for sdl for dev-c++
with some autogenerated code... defaults to a windows app though. :(
Anonymous CowardThis is refering to the earlier message by "ed", is it just me or isn't his spelling a bit horrendous
ikkiFor the VC++ 5.0 user: You must compile your own sdl.lib/sdlmain.lib/sdl.dll.
Download VisualC.zip from SDL's homepage (use search to find it...) and the source code, and you must have DirectX SDK (that's about 100 megs, get it from microsoft :) (buy on CD (not expensive) or download)).
Unzip VisualC.zip to the SDL directory, doubleclick on the SDL-(version)\VisualC\DSL.dsw, and build both projects on it (at this point, the DirectX headers/libraries must be in VC\include/VC\lib).
Just copy the SDL.lib & SDLmain.lib to VC/lib, and SDL.dll (for eg.) to WINNT/system32, or to the project folder. I, nor the program, for that matter, don't care.
And, it does make a difference whether you use the Debug or Release settings.
I don't know when the question was asked, or if this is a proper answer, or if anybody even cares anymore...
ikkiAnd (surprise surprise;), I forgot to mention: you must also have the SDL source.
bearAre there books on sdl
vovancan anybody tell me why while pressing more than 4 keys the key code of the SDL_KEYDOWN event (event.key.keysym.sym)= 0
does anybdy know how to make my program detect, for exammple, pressing of 10 keys
Piegum======================================================
C:\DEV-C_~1\BIN\ld.exe: cannot open -lSDLmain: No such file or directory
======================================================
im getting this linker error, can anyone help please
DorosIn DEVC++ 4.9.5.0,(and maybe in earlier versions) you can use the updater to get SDL set up perfectly, (It's under tools), then it will hand you the basic framework of an SDL program, which you can edit to whatever you wish.
-Doros
GulmuratNice tutorial, I love it! But please! Please use standart C rules where declaration of variables comes first, and routines come later. Kinda makes people confused, especially the beginners.
Chris WExcellent Tutorial.
Good overview of the basics. Though I'm not so sure about abandoning the pixel drawing routine to a ready made one that we don't "need" to understand.
Thanks
EJengineVery nice basic turorial.
I liked the Dev-C++ configuration steps.
Ninja JoeMake sure that SDL.dll is in your C:\windows\system, C:\windows\system32, and your project/executable file's directory also. NT and 2000 etc need it in system32 (this info is repeated from the beginning of the tut). If SDL.dll is in all of those folders at once, the redundancy will make sure the compiler and executable will find it when it needs it.
SDL_main is a routine in that DLL and the compiler will flag a link error as many of you are seeing.
CykXtremly good tutorial
I got dev-c++ and it cant link the code...
Ive got 5.0... Can someone help me
BrookYour code run on my PC very well. But when I pressed "Go
(Ctrl F5)" for debug the program , I got memory leaks.
My VC's debug output window print something like this:
----------------------------------------------------
Detected memory leaks!
Dumping objects ->
{20} normal block at 0x00980D70, 33 bytes long.
Data: < C > 00 43 00 CD CD CD CD CD CD CD CD CD CD CD CD CD
{19} normal block at 0x00980DC0, 40 bytes long.
Data: < |L > 14 7C 4C 10 16 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.
----------------------------------
How can I fix it
Dj VinceThx 4 explaining it so well.
Nice bassics. Love your tutorials.L
Keep up the good work.
I use MSVC++.
DevCpp give an error trying to built the .o file...
Under linux it doesnt work either.
I use KDevelop. Any other/better suggestion
Patrix_NeoHi! Saw your tutorial, but got compillation errors in your main. I dunno if it's gcc who is sissy about how things should be done, but I figured out that your "SDL_Surface *screen;" was "missplaced" (SDL v1.2.4, gcc 2.96-85). If you set it at the start of the main() I've got a better compillation result!
Hope this will shape up a thing or two
Nooneteachesbloodyc++roundhereim usin devc++ 4.9.5.0, aint working, lotsa errors.. linker errors.. bleh bleh bleh, damnit. :/
Read this first:http://www.libsdl.org/intro/usinginit.html
PallavHi!
Linker errors are obviously due to the library sdl.dll not being linked in compile phase. You need to add these libraries to the project to get it to compile.
As for the memory leaks, use SDL_FreeSurface to free the surface (screen etc) after you are finished. go to www.libsdl.org for documentation.
SatanicElfFor those having linker problems with Dev, make sure in project options you have: -lmingw32 -lsdlmain -lsdl
IN THAT ORDER. Also, make sure everything that came in the sdl lib folder is copied into your dev lib folder. SDL.dll needs to be in your windows system folder as well.
BTW, awesome tutorial. It helped me out a lot, but I was wondering if anyone knows where I can find an explanation for the DrawPixel function.
ScorpiwolfExcellent, its better than allegro
DeepZer0What an excellent work! This tutorial is really easy to understand!
I got a question though... does this work for LINUX C as well
NickReally good introduction to SDL.
Did anyone sucessful to make an app or game through SDL
KillerMonkI wrote a class, so that everything runs much more reliable. I'm working on the rest of the class, so that it will be able to have all the SDL functions in it. I'll distribute it when I'm finished.
GabbiAll I can say after that tutorial is: WOW something that works!! It amazed me to no end!! Every where I go , it seems to be a big conspiracy and no releases anything that works!
Thanks for this!
shaurzMy only problem is I need to drag the window off and then back into view for the image to appear. Other than that, very good. I managed to fix all errors myself.
SonarmanDeepZer0: Yeah it works great in any kind of UNIX, include Linux. You'll just need to install the sdl libs. To compile a one-file prog, do (at the command-line):
gcc file.c -o whatever `sdl-config --libs --cflags`
(the things surrounding the sdl-config stuff are backticks, to the left of the 1 key)
file.c is the name of the C source-code file, and whatever is the name you want the executable program to be called.
Hope that helped!
BluetigerCool Tutorial.Very good explanations.Thanx man!!
Troy W. S.Tutorial code worked flawlessly in DevC++ 4.9.6.0. Good instructions! Keep up the hard work.
yadav r.k.finally life is worth living, perfectly on 4.9.5.0 as well
yadav r.k.finally life is worth living, perfectly on 4.9.5.0 as well
Jeff Van BoxtelGreat tutorial. It was very helpful, thanks!
LifesummerWorked great on Dec-C++ 4.9.6.0
Thanks. I can finally do something other than console programs!
ptha Zathreagreat tut!
runs perfectly...
but I have a question... when I run the program.. I get this "loading bar" in the top.. why is it like that
Ålien ßoySweet tut!
the best one ive seen yet! I get no loading bar.
DeltreeA nice tutorial for a nice library.
Keep up the great job!
bingoboyCyk,
you could try upgrading to version 4.9.6.0
bingoboyCyk,
you could try upgrading to version 4.9.6.0
bingoboyCyk,
you could try upgrading to version 4.9.6.0
GrusifixThis is a nice tut. Could you make a nice little Dev-C++ Template Project to download. Can you tell where to get other SDL libraries for dev-c
KackurotIs sdl easier than directdraw or dirct3d
TazarKackurot: yes SDL is tons easier than DirectX.. compare a basic SDL window with a basic DirectX one and you'll get the flow :)
KackurotHow do you get rid of the console. When the prog starts the console is behind the window.
NitrocloudThis is a nice tutorial... I had been using the Allegro libraries before because they are the easiet I've seen for graphics, but this seems like I may get the hang of it. This has a better video quality because there is less flickering in the screen. If you don't understand this, the example that DEV-C++ V 5 BETA has when upgraded that is nice.
x0termyay! great work man.. I've also been using Allegro for some time, but i'm starting to get the hang of SDL too now.. Keep up the good work!
TylerI get this error when running the code after we add the loop...
g++: installation problem, cannot exec `ld': No such file or directory
GI GERMwhen i compile in devc++ version 4.9.6.0 i get a WHOLE LOT of linker errors
GI GERMwhen i compile in devc++ version 4.9.6.0 i get a WHOLE LOT of linker errors
GI GERMwhen i compile in devc++ version 4.9.6.0 i get a WHOLE LOT of linker errors
willburi hate macs but i use them in school, so i was wondering if this works on them.
whateverin devc++ i have sdl.dll inside C:\Dev-Cpp\lib but when i try to run my sdl app it say that it is missing
whateverin devc++ i have sdl.dll inside C:\Dev-Cpp\lib but when i try to run my sdl app it say that it is missing
JustinOk.. I just started with SDL, using Dev-Cpp. After writing out the first demo program you have, before the graphics are added in, I get these errors.
Line 6: type specifier omitted for parameter
Line 6: type specifier omitted for parameter
[Warning] In function 'int main(int char **)':
Line 24: 'Null' undeclared (first use this option)
Each undeclared identifier is reported only once for each function it appears in.
[Build Error] [main.o] Error 1
Any help with this
JustinIt cut me off.. but please Email me at GemstoneAuctioner@hotmail.com, or post here in response. Thanks.
FiskHi!
When I try to compile this code in VS.NET I get this error: fatal error LNK1561: entry point must be defined
I have included both the .lib files, I have also changed the code generation to multi-threaded DLL. Can Anyone please tell me what I´m doing wrong
FiskHi!
When I try to compile this code in VS.NET I get this error: fatal error LNK1561: entry point must be defined
I have included both the .lib files, I have also changed the code generation to multi-threaded DLL. Can Anyone please tell me what I´m doing wrong
norcarbawhat does it mean when it says "undefined reference to sdl_main"
norcarbayes, i have -lmingw32 -lislnmain -lisdl in project options.
celloboythis seems to be the general SDL forum... i'll post my problem here. I made a pong clone, which compiles and runs on linux just fine. I recently compiled it on windows with ms visual c++; it compiles and links, and builds an exe. The strange thing is that it only works when i select "run pong.exe" inside ms visual c++; it won't run directly from an explorer shell or the command prompt. any ideas
lucaswIn dev-cpp 4.9.7.0, I had to add the include directory C:\Programming\Dev-Cpp\include\c++\mingw32 - otherwise there c++config.h isn't found. This is was for a c++ project that uses the stl heavily.
lucaswIn dev-cpp 4.9.7.0, I had to add the include directory C:\Programming\Dev-Cpp\include\c++\mingw32 - otherwise there c++config.h isn't found. This is was for a c++ project that uses the stl heavily.
<a href="mailto:coldshift@gdnmail.com">ColdShift</a>I just finished reading the first of your tutorials - it's -excellent- and far better than trying to code in DirectX. Wondering how to kill the console box though, I tried using a Win32 App project, but then the program just quits... mm... I'm sure I'll find out in future tutorials. Excellent.
<a href="mailto:isergiu@home.ro">ISergiu</a>This is realy a great tut. I had linker problems in Dev c++ but adding -lmingw32 -lSDLmain -lSDL in the liker options was a good fix. The first fragment of the tutorial is critical.
To eliminate the console window select "Do not create console" in Dev project options. If you don't use Dev it might be o good ideea to create a windows project(#include <windows.h> instead of stdlib.h) and use
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
}
as your main function(you don't need to understand it, unles you want to lern Win32) .
RetroIf wanting it in win32 executable add
#include <windows.h> to the beginning of the file and change the int main bits. See ISergiu post
Retro#include windows.h
drslinky1500celloBoy
I don't work in Windows a lot, but I have had that same trouble with Kylix in Mandrake. Windows probably doesnt know where the dll is to link it to the program. It probably needs to be in System or System32 file. (I think thats what they are, been a long time since I windows programed). I know in linux I edited ld.so.conf, I don't know if there is a similar file in windows or not. Hope this helped.
Q-bertGreat tutorial...so far...had to use Dev C++ 4, as I
ran into some problems with the version 5 beta (Much cleaner IDE).
jasonI liked it a lot, I changed the code a bit so you don't have to allocate extra memory for the int done, saved some bits here is the code.
while(1)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { return 0; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { return 0; }
}
}
DrawScene(screen);
}
Yaroslav DvinovI did all the necessary steps and put all the .DLL files in the right place. When I build the code, it compiles fine, but doesn't show the window on the screen. I use Windows XP and VC++ 6.
Yaroslav DvinovI did all the necessary steps and put all the .DLL files in the right place. When I build the code, it compiles fine, but doesn't show the window on the screen. I use Windows XP and VC++ 6.
f2oI've tried SDL on linux and now in win with dev4. it seems that works fine but it gives me an
atexit(SDL_Quit); -> error
implicit declaration of function `int atexit(...)'
I don't know why!
great job!
ciao
kungf00very good, thought me alot!
mikeim getting this error message in visual c++ 6
MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib "msvcrt.lib" conflicts with use of other libs; use /NODEFAULTLIB:library
any ideas whats causing this
mikedoh changing from multithreaded dll to debug multithreaded dll sorted the problem :)
btw thanx for this tutorials they look good
MouseSkateri figured it out! ok well there's 255 colors in RGB, so if you leave the last feild just y y and x it will make a box 255 by 255. the first 2 parameters will darken from top to bottem while the 3rd will go left to right. sooo if you wanted to make the box 800 x 600 you would do.. (x, y, y/2.35, y/2.335, x/3.13). but it gets better, to change the different colors leave 1 of the fields constant! for example..(x, y, y/2.35, 0, x/3.13). that would mean that blue would darken from left to right and red would darken from
MouseSkateri figured it out! ok well there's 255 colors in RGB, so if you leave the last feild just y y and x it will make a box 255 by 255. the first 2 parameters will darken from top to bottem while the 3rd will go left to right. sooo if you wanted to make the box 800 x 600 you would do.. (x, y, y/2.35, y/2.335, x/3.13). but it gets better, to change the different colors leave 1 of the fields constant! for example..(x, y, y/2.35, 0, x/3.13). that would mean that blue would darken from left to right and red would darken from bottem to top and they will mix together in the middle to make a nice purple. experiment! (sorry if i double posted i accidently hit send to early, doh)
KiboGreat tut.. first one(graphics) i can follow step by step.
tuobbi-89Great tutorial! The example works well, so thanks! :)
Constructive CriticismYou can put DrawScene(screen) *before* the while loop and only execute it a single time. The video buffer is kept in memory and redrawn for you by the system whenever it is covered and uncovered by other windows, meaning you don't have to keep drawing unless you want to vary the parameters (i.e. as a function of time). Should knock CPU usage way down. ; )
totoowhen i have included stdlib.h i get this error:
error LNK2005: _exit already defined in LIBCD.lib(crt0dat.obj)
and a couple more of that kind.
When I NOT have stdlib.h included I get this error:
error C2065: 'exit' : undeclared identifier
MSVC6.0
thanks (and I hope you can understand my bad english)
HauptmannYeah!
I am from Austria and this is a very good Tutorial.
No german tutorial was so good!
m3ntolit works fine. Thanks a lot for share your knowledge
MikkSa võiksid need kommentaarid mingi eraldi lingi alla panna. Neid on nii palju ja nad segavad. Tee kuidagi nii, et kui ületab 10 kommentaari, tuleb next nupp.
Aga eks sa ise tead sinu leht :)
VivamedGood Job, you've got my great congratulation !!!!!!!
TypoI have been using Allegro, and while it has been easy to use, it seems to be slow. Your tutorial on SDL is the first one I've seen that actually helps me to learn how to use SDL with DEV-C++.
balaIts really good job. The tutorial was wonderful and very useful for learning SDL. But I need compele SDL tutorial like this. Anybody Please help me in this regard.
balamy mail ID is netinbala@hotmail.com, Please help me.
Robo JinLINK : fatal error LNK1104: cannot open file "sdl.lib"
Error executing link.exe.
The problem with this error is that I have it included
in the object/library section. Is that file in 1.2.5a for VC++ corrupt or something
Robo JinFixed it by adding the two .lib files to the main lib directory
dp1aGracias!!! Un muy buen tutorial.
JoelWow, it works! Thanks for the great tutorial that finally makes some things make sense! Make sure you name your files .cpp and not .c if using VC6, it helps a lot =)
m0nki3You're totally the best ;)
Hmm..sdlmain.lib(SDL_main.obj) : error LNK2001: unresolved external symbol _SDL_main
Debug/SDLtest.exe : fatal error LNK1120: 1 unresolved externals
kevinI also got the code to work perfectly with Visual C .NET. Slight modification required to the compiler settings, but no code modification necessary.
kevinsorry, i'll try to be more useful:
Project Properties: Propety Pages: Linker: General -> Additional Library Directories. Add your SDL-xx.xx.xx/lib path here. Just adding the libs to the project won't work.
Please help meWhen i try to run your tutorial 2 code for the box on the back ground i get this error.
--------------------Configuration: instruct - Win32 Debug--------------------
Compiling...
main.cpp
c:\microsoft visual c++ 6.0 standard edition\vc98\include\instruct\main.cpp(10) : fatal error C1083: Cannot open include file: 'SDL\SDL.h': No such file or directory
Error executing cl.exe.
instruct.exe - 1 error(s), 0 warning(s)
The thing is is that i did what it said for me to do and i have the SDL folder with SDL.h! and still i cant get this thing to work and i really want to use it bad so could some one please help me out here i did everything i could
mikeI'm using KDevelop 2.0, and when compiling, I get this error:
undefined reference to `SDL_MapRGB' , also for SDL_LockSurface, SDL_UnlockSurface, SDL_UpdateRect, SDL_SetVideoMode, SDL_Init, SDL_GetError, and SDL_Quit. Any ideas
mikebtw, "Please help me", your problem is that when he says "SDL\SDL.h", he means use either "SDL" OR "SDL.h".
xaxaHey! This also work on linux, kewl!
You are good teaching stuff. Thanks. :)
LuQWow, it work well, i used OpenGL before and this is more simple than OpenGL, one question, which one is better OpenGL API or SDL API, can i combine both ...
i have some advice on event handler :
SDL_WaitEvent(&event);
switch ( event.type )
{
case SDL_KEYDOWN :
if ( event.key.keysym.sym == SDLK_ESCAPE )
{
done = 1;
}
break;
case SDL_QUIT :
done = 1;
break;
}
JayeI couldn't get SDL to install before... but you make life so much easier!
http://jo-soulutions.com
MurphyGreat! its works perfectly in my slackware linux whit gcc file.c -o whatever `sdl-config --libs --cflags` thanks Sonarman!
MaxmikeHOLY FUCK YES. I have been looking for a simple pixel drawing function for over 20 hours, I have delved into assembly, far pointers, near pointers, open gl, windows programming, other compilers. THIS is the easiest library set to plot pixels with EVER. I didn't want to learn windows programming and I didn't want to use most of the functions opengl sports, this is the best. ever.
Christian WizzardGREAT. Just what i needed for FAST starting SDL / GL with DEVCPP.
Thanx!
Chris
www.wizz.de
ErikGood. Thanks.
Erik
Daarkonhmmm ... I'm trying to adapt my C version (made w/TurboC graphics.h under windows and svgalib under Linux) of the "Life Game" of Conway ... Compilation works well with Dev-C++ 4 (except when Dev-C++ says "gcc: -lmingw32: linker input file unused since linking not done
gcc: -lSDLmain: linker input file unused since linking not done
gcc: -lSDL: linker input file unused since linking not done ; same thing for -lSDLmain and -lmingw32)
, but the SDL window crashes and nothing is drawn into !!
what can I do to solve this problem
J3$U$H1M$3LFomfg, may i say, waw
PasqualeWhen a try to execute this tut, the shell says:
tut: tut: connot execute binaty file
What i can do
wizzardchange the file permission to 755 using:
'chmod 755 tut'
that wil make the file executable on *nix...
rafalI have one problem. Example which was written in this tutorial compiles but it can't be linked. I have that message:
Linking...
sdlmain.lib(SDL_main.obj) : error LNK2001: unresolved external symbol _SDL_main
Debug/Grafika.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
I think that I do everything which was said in tutorial, so what do I wrong
scebadoffthis was my first experience with graphics programming. It compiled, first time, with no linker errors! Elation only begins to describe my feelings. Good Job! I look forward to doing the rest of these tutorials.
James Fdrslinky1500
>Windows probably doesnt know where the dll is to
>link it to the program. It probably needs to be in
>System or System32 file.
Yes that's right, it should be in system or system32, or it can also sit in the directory where the executable runs in
>I know in linux I edited ld.so.conf, I don't know if
>there is a similar file in windows or not. Hope this
>helped.
well, you could edit the system's environment variables (that's what ld.so.conf is right
James Fgah.. it chopped me off.. neway all i was gonna say is, you *could* edit the environment variables - namely the "path" variable and add the path to the dll - but that would be needlessly complex ;)
EmreA way to compile in LINUX
g++ -o lesson1 lesson1.c -lSDL
fuxxA better way to compile in LINUX
g++ -o lesson1 lesson1.c -lSDL -lpthread
dead_soulVery nice tutorial. I helped me alot :P
Inf3rnalOkay, when I compiled the code at the end of the tut, i got the following errors:
--------------------Configuration: Sdl1 - Win32 Debug--------------------
Compiling...
Sdl1.cpp
Linking...
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_LockSurface
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_UnlockSurface
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_MapRGB
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_Flip
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_PollEvent
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_SetVideoMode
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_Quit
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_GetError
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_Init
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/Sdl1.exe : fatal error LNK1120: 10 unresolved externals
Error executing link.exe.
Sdl1.exe - 11 error(s), 0 warning(s)
I have no clue what any of that means......email me at inf3rnal@msn.com if you can help
Inf3rnalOkay, when I compiled the code at the end of the tut, i got the following errors:
--------------------Configuration: Sdl1 - Win32 Debug--------------------
Compiling...
Sdl1.cpp
Linking...
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_LockSurface
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_UnlockSurface
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_MapRGB
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_Flip
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_PollEvent
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_SetVideoMode
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_Quit
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_GetError
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_Init
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/Sdl1.exe : fatal error LNK1120: 10 unresolved externals
Error executing link.exe.
Sdl1.exe - 11 error(s), 0 warning(s)
I have no clue what any of that means......email me at inf3rnal@msn.com if you can help
Inf3rnalOkay, when I compiled the code at the end of the tut, i got the following errors:
--------------------Configuration: Sdl1 - Win32 Debug--------------------
Compiling...
Sdl1.cpp
Linking...
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_LockSurface
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_UnlockSurface
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_MapRGB
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_Flip
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_PollEvent
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_SetVideoMode
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_Quit
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_GetError
Sdl1.obj : error LNK2001: unresolved external symbol _SDL_Init
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/Sdl1.exe : fatal error LNK1120: 10 unresolved externals
Error executing link.exe.
Sdl1.exe - 11 error(s), 0 warning(s)
I have no clue what any of that means......email me at inf3rnal@msn.com if you can help
test<iframe width=300 height=10000 href=http://google.com src=http://google.com></iframe>
Inf3rnalWTF!
test 2<iframe width=300 height=10000 href=http://google.com src=http://xxx.com></iframe>
Thormmmm-m :-) This is a really usefull and easy-to-understand tutorial.. after finishing it i was able to hack my brains out all night in the graphical world !! I love it :-D
TobyMost PC keyboards will only register four key presses at the same time. So you have no hope of getting 10 keys simultaneously registering. Now if you have a decent joystick, you might be able to read all 10 buttons at the same time... but no guarantees.
BlackmanIf you want more than four keys to register at a time, use the keypress to set a global bool value and only reset it after the '10' key event has fired, you will actually only register each key individually but when your program checks the bool values it will seem as though all 10 keys are active at once. Just a thought from a newbie.
JekutoAbout SDLmain.lib(SDL_main.obj) : error LNK2001: unresolved external symbol _SDL_main:
change int main()
into int main(int argc,*char argv[])
I dunno what's that mean.but it solved the problem.
AnonymousHmm... Jekuto, int main(int argc, *char argv[]) <- you wrote that, but it should be:
int main(int argc, char *argv[])
hehe, i know, everyone make typos =)
EleniumThanks Jekuto, you solved my problem.
TypoWell, finally got around to installing Dev-C++ and the SDL zip file above. Pasted in the tutorial deleted some extra stuff, and the tutorial compiled and ran perfectly the first time! No errors! Now, as I understand it, SDL will try to use DirectX if it is available. How can I find out if it is being used or not, and do I need to install the directx SDK
zenaphexJekuto:
"int main(int argc,*char argv[])" is for like when you use command-line arguments. For example "format c:". "format" is one argument (being the filename) and "c:" is another (being the value to be passed to a function).
tr0nixReally cool thing ;) I got it run with KDevelop and the gcc3 compiler on Leeenukz.
PwipwiFor the guy who asked how could 4 keys or more could be detected at the same time, I may have a solution which consists in creating a bool array[256] which remembers which keys are pressed...
ChuftyThe problem with the linking is that the Dev 5 beta interface is different to the Dev 4 interface. You say to put the arguments in "Further object files or linker options", but there is no such option anymore. I've tried adding this line to every viable looking field and I still get the old 'no entry point' error. Any ideas
nicozeFirst, thx for this great tutorial and numerous comments added.
Then a question. I use Dev c++ 4 and I have a problem when I try to declare more than 3 SDL_Surface in my program. It compiles but a "windows" error occurs when I try to execute it. Does anyone has ever managed to use several SDL_Surfaces in the same time
SouthEastMexicoBoyI want to master SDL because i need to get some ideas out of my head. This tutorial is easy to understand and it works just fine. i'll read them all!
Keep up the good work!
SubLogictutorial helped alot.. thanks.. i hope the other tutorials are as easy to follow as this one :) nice job
Maarten WeynReally great tut, thx a lot
thegrogenGreat tut, but I get a linker error like this every time I try to compile in Dev-C++:
[Linker error]undefined reference to 'WinMain@16'
What the heck
Tom 7Thanks, a good intro!
leollA very good tutorial!!!
Thanks.
Fox112Great Tutorial! SDL definately ROCKS! keep coding
ShakuanHey thanx this is great stuff for newbs like me.
radawayVery nice tutorial.
I am however getting this error using dev-cpp version 4.9.8.1:
[Linker error] undefined reference to `__gxx_personality_v0'
I think this might be cause of version 3.2 of gcc I dont know, can anyone please help me.
radawayVery nice tutorial.
I am however getting this error using dev-cpp version 4.9.8.1:
[Linker error] undefined reference to `__gxx_personality_v0'
I think this might be cause of version 3.2 of gcc I dont know, can anyone please help me.
GrellinGreat job. You closed definately helped to shorten the learning curve.
KTakhisisGood job, easy to understand and clean code. :)
FaladosI didnt read all the comments, but for you people who are getting errors in DevCpp about console_main, undefined references to SDL_main, try changing your "main" function to "SDL_main"
Andrei KravtsovHi! I'm Andrei. I use Borland C++ 5.1. And i can't go through GFX with SDL tutorial.
I have installed the files "sdlDevCPP-1.2.4.zip" and tried to run yhe first example
Borland says :
-Error: unresolved external '_SDL_MapRGB' referenced from module...
-Error: unresolved external '_SDL_LockSurface' referenced from module...
-Error: unresolved external '_SDL_UnlockSurface' referenced from module...
-Error: unresolved external '_SDL_Init' referenced from module...
-Error: unresolved external '_SDL_Quit' referenced from module...
-Error: unresolved external '_SDL_SetVideoMode' referenced from module...
HELP! What should I do to fix it
Andrei KravtsovP.S. My e-mail is andruss2001@hotmail.com
QzarThank you i have been looking for a good tutorial that worked with Dev-cpp for weeks.
MarioHey. Firstly, ur Tut is pretty good...some is a little confusing, but I get most of it. Secondly, I did exactly what you said, and then figured out that I had put -Imingw32, etc in the wrong box (As I am using
Dev-C++ 4.9.8.0), and now I have turned 11 errors into 2. the two are the same, [Linker Error] undefined reference to 'WinMain@16'...I tried adding some different libraries, but it didn't work...can anyone help
Mario..can anyone help
StillMariohelp
AsAboveKeeps getting cut off! neway...email: mario_samperio19@hotmail.com
StillMariohelp
KTakhisisI switched from a windows 2000 machine to a windows XP machine. I'm trying to compile the code for the tutorial that's include in the zip file. It worked for 2000 but not for XP. It compiles fine, but now when i run the exe file, it now gives off a memory access fault error. Any one has any ideas
RaieThis is probably the most helpful tutorial i've seen, all the others never worked for me. Keep it up!
Ben'WinMain@16' errors are sometimes caused if you declare "int main()" as so. It needs to be declared as:
int main(int argc, char *argv[])
Hope this helps
LarsQuote:
>Gulmurat
>Nice tutorial, I love it! But please! Please use
>standart C rules where declaration of variables comes
>first, and routines come later. Kinda makes people
>confused, especially the beginners.
The programs are written in C++, and in C++, you should *not* put all the declarations in the beginning.
Pretty nice tutorial, I am off the read the others now. Just a little note:
Your Slock and Sulock-functions are prettu unecessary. They add several function-calls to the code, as well as unecessary lines of code. Simply calling SDL_Lock(screen); and SDL_Unlock(screen); is sufficient, and it doesn't hurt to lock the screen even if you don't have to.
CalahornGreat friggin Tutorial. I've now made my own classes to help me with initialization and have made some of the commands easier and am ready to program. Way to go on a nice tutorial.
RinoOK I'm using Dev-C++ under win xp. I did everything just like this tutorial said and I keep getting the error:
C:\DEV-C_~1\Bin\ld.exe: cannot open -lmingw32 -lSDLmain -lSDL: No such file or directory
Does anyone have an idea
dudeexcellent basics, know everything about basix naw (:, good job! going to the next tutorial becoming a game programmer :D
PHLi must create Win32 Console App project.
Win32 Application project is not working, because Win32 Applications require WinMain function !!!
so i think DirectX is better than SDL
PHL_is_an_idiotOf course DirectX is better than SDL! but if you're basis for that is because you see DX use WinMain, then you must be an idiot, because SDL uses WinMain it just so happened that it handled all the messy window-creation stuff inside the DLL.
kvGreat tutorial Marius.
Gets you started in minutes! Now on to the next one.
Thanks a lot.
ArkainHello , I have a very big problem with my program code:
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
// The functions are not shown to save space
void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B);
void Slock(SDL_Surface *screen);
void Sulock(SDL_Surface *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);
}
// DRAWING GOES HERE
return 0;
}
when I compile it I don't have any error, but when I build and run it, it says to me:
--------------------Configuration: snake - Win32 Debug--------------------
Linking...
snake.obj : error LNK2001: unresolved external symbol _SDL_SetVideoMode
snake.obj : error LNK2001: unresolved external symbol _SDL_Quit
snake.obj : error LNK2001: unresolved external symbol _SDL_GetError
snake.obj : error LNK2001: unresolved external symbol _SDL_Init
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/snake.exe : fatal error LNK1120: 5 unresolved externals
Error executing link.exe.
snake.exe - 6 error(s), 0 warning(s)
CatGreat tut!
The best way to setup library is to read readme files and
helps. Doing EXACTLY what it says there will give you
what you want!
Arkainok...thank
Arkainbut i have the same errors.
PHLHey, you... read the SDL docs. SDL REQUIRES DIRECTX SDK HEADERS (under WINDOWS). but for example, allegro (another graphics library) can use main() function even width Win32 Application !!!
PHLin the first message i was "saying" this: i "don't like" WinMain function, so SDL is useful for programming graphics without WinMain. But it works only with console, and i hate consoles.It is better (for me) to use DirectX with WinMain and WITHOUT CONSOLE than SDL with main but WITH CONSOLE. but that idiot will not understand it.
SkullBiterTo the dudes having trouble with undefined reference to `__gxx_personality_v0 try using g++ instead of gcc. I don't use DevC++ (I use unix) but I think it will involve enabling c++ at some point, letting it know that you dont want straight c.
Yes!I hope no one has explained this. To figure out the DrawPixel thing, note that the last three values is R,G,B and is of Uint8. 0 means no color, 255 means full color. Now try setting to 0,0,255 and you'll see.
Help me please!The error i'm getting...... "The SLD1.exe file is linked to missing export SDL.DLL:SDL_SetModuleHandle" I'm using dev c++ 4. something, with windows 98, and pretty sure i've added all the linker files i was told about. help!
NvmNevermind, ignore the 'help me please'. I followed one of the links i found from earlier up the page, and that gave me a new run-time lib. which works great. Wicked tut., keep up the gd work! :)
PHL_DuhhYou're the one who needs some real reading. SDL doesn't work only with consoles, DUHH, SDL doesn't required DX headers unless you actually are going to use DX, DUHH!
BlacKcuDlooks nice, works nice. great tut !
Jonquick tip to speed up the initial draw. Split up the DrawPixel function into 4 seperate functions DrawPixel_8/16/24/32 . Then in the main loop, test for the bit depth and set a function pointer to the correct one :
typedef void (* DrawPixel_f)(SDL_Surface *, int, int, Uint8, Uint8, Uint8);
DrawPixel_f dp;
switch (screen->format->BytesPerPixel) {
case 1: /* 8bpp */ dp = DrawPixel_8; break;
case 2: /* 16bpp */ dp = DrawPixel_16; break;
case 3: /* 24bpp */ dp = DrawPixel_24; break;
case 4: /* 32bpp */ dp = DrawPixel_32; break;
default: dp = DrawPixel_8;
}
the function pointer dereference is cheaper than the cost of the switch/case in the current DrawPixel. Multiply the saving by 640x480xhowever many times you redraw the screen.
More details of fp's at www.function-pointer.org
Thanks for the tutorial!
CozFor the people with the Winmain@16 problem, I discovered that the libraries NEED to be linked in the order that the tutorial says so if you have -lSDL -lSDLmain -lmingw32 (like I had :P) it won't link.
konartiseh
konartisthey dont need to be linked in any certin order in paticular, but sdl, as well as opengl should be at the beginning anyways to help prevent this disaster
JonI've found that with the newest mingw/msys from sourceforge, with the sdl libraries built from source, the build order does matter. something like
gcc -o <target> `sdl-config --cflags` file `sdl-config --libs`
works, but only with the cflags before and libs after (or vice versa!)
JonPHL, FYI I can compile SDL apps in windows, using main() rather than WinMain without having a console.
Neal Anyone seen this
PHLJon: But i can't
[DC*3]Jp^UkCan anyone tell me how to split the screen up into 2 halfs the into 4 parts with different colours
[DC*3]Jp^UkCan anyone tell me how to split the screen up into 2 halfs the into 4 parts with different colours
DaveJekuto, I love ya man. After days of trying to get SDL to work, your commment about int main() fixed it right and good. Thanks!
DaveJekuto, I love ya man. After days of trying to get SDL to work, your commment about int main() fixed it right and good. Thanks!
Dave's Momgo to www.cplusplus.com for some nice info on the libraries
yusucothx for your article
lilbastidHey, Thanks a lot for the tut! Mucho Grassy Ass!
DrakkconSome guy near the top of the page asked if it was possible to mix SDL and OpenGL. The answer....YES! Dev-cpp v5 has a template for it! Got a good frame rate too (this could be becuase I use a Geforce4)!
But I've a question, is all the rendering done in SDL software, or is it mostly hardware acceleration. I'm (not so succesfully) making a 2d sprite engine, and thought hardware acc. isn't neccesary, I still think I could churn a few FPS out. Thanks for any help!
wow<img src="http://cone3d.gamedev.net/cgi-bin/resist.pl">
gamkillerI'm not using devc++, but this tutorial was pretty interesting. I've used svgalib and such for years, but this tut was what I needed to get going with SDL. Ta.
Java DukeTo compile this on Project Builder on OS X, you need to declare the int's for x and y outside of the 'for' loops in the DrawScene function.
ngo_canhThanx
errordev-c++ 4.9.8.0
getting this error:
[Build Error] [test.exe] Error 1
Armond SarkisianAwesome Tutorial, Thanx
Tamanice!!
BetelgeuzeBeen trying to learn graphics for almost a week now, Great tut, man! thnx
Jokera nice tutorial. But is there anyone who knows to get debugging working within DevC++
hardcoderJoker, have you checked linker options
FlippyNice Tutorial!
Merkothgetting a linker error: something "undeclared to Winmain@16" any ideas
Merkothi'm working with dev-c++ 5 beta 8.
righti get the same prob with both dev 5 beta 8 and dev 4. It works fine for me though on msvc6. Nice tut!
Merkothbuy i don't have msvc! :(
I'm using VC++ .net 2003 and I get these errors.
main.obj : error LNK2019: unresolved external symbol _SDL_Quit referenced in function _WinMain@16
main.obj : error LNK2019: unresolved external symbol _SDL_GetError referenced in function _WinMain@16
main.obj : error LNK2019: unresolved external symbol _SDL_Init referenced in function _WinMain@16
Debug/SDL_C3D.exe : fatal error LNK1120: 3 unresolved externals
ALso is it supposed to be a Win32 application or a Win32 Console Application. I have to put the WinMain() or it will give an error in Win32 Application
UnknownI can't get SDL to work with VC++ .net 2002 standard edition. I get 2 errors all the time:
SU_SDL error LNK2019: unresolved external symbol _SDL_main referenced in function _main
SU_SDL fatal error LNK1120: 1 unresolved externals
I redid the steps all over again and even reinstalled the VC++ but it doesn't work.
UnknownI solved my problem. You need int main(int argc, char *argv[]), as someone already posted.
jussi05@hotmail.comWhat that all "Sulock" thing means
screen unlock, some monitors needs to be unlocked to be able to draw on
Some problems here:...I tried to separate the functions from the main file like this:
Main.cpp:
#include "engine.cpp"
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;
}
Engine.cpp
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B);
void Slock(SDL_Surface *screen);
void Sulock(SDL_Surface *screen);
void DrawScene(SDL_Surface *screen);
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);
}
But it gives me 5 erros:
inking...
main.obj : error LNK2005: "void __cdecl Slock(struct SDL_Surface *)" (
gnatinatorNice tutorial. Plotting a pixel is sure complex in SDL, when compared to Allegro.
Problem...How can I separate this functions and used them on another file
Problem...How can I separate this functions and used them on another file
NecromancerEnd of all, i've done lesson :)
I wanna add something about initializing SDL to Dev C++. im using 4.9. This is for who can't instal truely sdl and get "unresolved external symbol" errors.
When you open a new console project [or win app, nothing changes], you should go to Project Options from project menu, and select Linkers area from 3rd or 4th tab panel. and you'll add -lmingw32 -lSDLmain -lSDL seperately. one command for one row... i advice to paste them, not write, l is lower L, not capital i... end of all, it have to work... ;)
Alezehhh errors...
--------------------Configuration: dsg - Win32 Debug--------------------
Compiling...
vcn.cpp
Linking...
vcn.obj : error LNK2001: unresolved external symbol _SDL_LockSurface
vcn.obj : error LNK2001: unresolved external symbol _SDL_UnlockSurface
vcn.obj : error LNK2001: unresolved external symbol _SDL_MapRGB
vcn.obj : error LNK2001: unresolved external symbol _SDL_Flip
vcn.obj : error LNK2001: unresolved external symbol _SDL_PollEvent
vcn.obj : error LNK2001: unresolved external symbol _SDL_SetVideoMode
vcn.obj : error LNK2001: unresolved external symbol _SDL_Quit
vcn.obj : error LNK2001: unresolved external symbol _SDL_GetError
vcn.obj : error LNK2001: unresolved external symbol _SDL_Init
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/dsg.exe : fatal error LNK1120: 10 unresolved externals
Error executing link.exe.
dsg.exe - 11 error(s), 0 warning(s)
NotheroReally nice ez to follow lay out , Good work
Newbie to SDL... Please helpI have found a graphics system called pygame...its for the python language..and development is fast and easy. Help me out.. i dont know whether to use VC++ or Python...they both seem powerful, but which is more powerful
D1Thanks a lot. My very first stepping stone into the world of graphics. I know I haven't done much at all, but the first step is usually the hardest.
Jeff SchillerExcellent tutorial for a beginner...I'm coming from a heavy DirectX background and I must say that I'm impressed with the ease by which you can quickly get something accomplished using DirectSDL.
DrakkconUhhh, SDL is in no way related to DX. It's not DirectSDL, it's just SDL.
Conrad@LostReaction.comI've never done a single thing with GFX programming with C++ before, this tutorial was very simple and worked perfectly! My friend convinced me to try SDL over DX, I asume DX is a bit tougher. SDL is so straight forward and simple, it's great and I hope to move on to more advanced SDL concepts like SDL_net!
NFSpeedervery good this tutorial...can you do this tuorial in portuguese
ZadrraSGreat tut, but i get and error: "fatal error C1010: unexpected end of file while looking for precompiled header directive" How do i fix it
psyphenIf you encounter errors relating to conflicts with certain libraries during a build in VC++6 in that you receive conflicts with '_atexit' (or similar) and so on, you might need to consider ignoring msvcrt.lib and libcmtd.lib such as Project Settings -> Link -> Category: Input -> Ignore libraries: msvcrt.lib libcmtd.lib
Also, if you feel it's not necessary to place sdl.lib and sdlmain.lib at the end of the Object/library modules: line and are planning to use additional libraries and include files, then add the relevant pathnames to Tools -> Options... -> Directories -> Show directories for: Library files (for paths ending in 'lib') and Tools -> Options... -> Directories -> Show directories for: Include files (for paths ending in 'include')
psyphenSometimes the unresolved external symbol errors can often be solved by setting up your linking options properly. However, you may need to include the following lines at the very start your source file if you still believe you have it correctly set up, but you continue to receive the same (or similar) errors:
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
psyphenIf you experience a fatal error which says "Cannot open include file: 'SDL/SDL.h': No such file or directory", you may need to change the following line..
#include <SDL/SDL.h>
..to..
#include "SDL.h"
lazercheezF*ing perfect tut. Where's your PayPal Donate button
kspri noticed i get errors if i dont add the int argc, char *argv[] as parameters to main() in dev-cpp while using sdl. weird
PedroVery good tutorial! Congratulations!!! :))))
Bravo!!! :D
BazThanks for a quick and simple tutorial. Now i can do what i've been wanting to do for along time in a windows environment.
clappsNice stuff! One question: Is there any way to make it work under Linux
CapriYouthyeah, I got to say, this tutorial cuts good....
I don't know if it's my stupidity or if it's mentioned before, but there's a tiny small thing missing in the forloop, when colouring the screen.
( I might be wrong, but when I added it, it worked )
for(int i=0;i<100;i++) instead of for(i=0;i<100;i++);
For when someone even n00bier than me comes along and doesn't figure it out ;)
CapriYouthyeah, I got to say, this tutorial cuts good....
I don't know if it's my stupidity or if it's mentioned before, but there's a tiny small thing missing in the forloop, when colouring the screen.
( I might be wrong, but when I added it, it worked )
for(int i=0;i<100;i++) instead of for(i=0;i<100;i++);
For when someone even n00bier than me comes along and doesn't figure it out ;)
MythHe i was looking for something like this.
I am bad in graphichs programming...
I use borland C++ builder 6, i also can compile just C.
But it can't find SDL/SDL.h that file just isnt there.
How to get it there
jachorThats my version of DrawPixel:
void DrawPixel(SDL_Surface *surf, int x, int y, unsigned char R, unsigned char G, unsigned char B) {
Uint32 color = SDL_MapRGB(surf->format, R,G,B);
memcpy((char*)surf->pixels+(y*surf->pitch)+(x*surf->format->BytesPerPixel),&color, surf->format->BytesPerPixel);
}
It works under my Mandrake Linux with SDL 1.2.5
XKpenice tutorial, good for a starter see whow the lib works.
btw, pretty pixel function jachor :)
thegrogenNice tut.
This is probably the easiest game API to use in the world. It's like an easier DirectX.
Actually, it is DirectX...
DirectMedia is part of DirectX
thegrogenBy the way, how do you get rid of the cursor
SamExcellent.
CjmovieI did exactly what you said, but I got this error two times:
Und
[Linker error] undefined reference to `WinMain@16'
john draytonits works just fine. keep it up!
ShionaThis is great tutorial as most of the people have allready told... but this thing seems to use 100% of my CPU and that isn't propably what it's meant to do... have i done something wrong or did i miss the comment where was the solution
gudzuthanks for tutorial
HeikkiVery good tutorial! Thank you!!!
JemAwesome stuff, thanks very much. However in VC++ 6 is there a way to use the code completion for the SDL arguments
BobIn devC++ I do not see a "Further object files or linker options" to put the line "-lmingw32 -lSDLmain -lSDL". Although I assume I should put it under "Linker parameters" (I am using devc++ v. 4.9.8.0) However, when I go to compile I get the errors " [Warning] In function `DrawScene', 74
`for' loop initial declaration used outside C99 mode, 76
`for' loop initial declaration used outside C99 mode
and this was copy and pasted straight from the tutorial
sfx1999The C99 mode error can be fixed by changing
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);
}
To:
void DrawScene(SDL_Surface *screen)
{
Slock(screen);
int x;
for(x=0;x<640;x++)
{
int y;
for(y=0;y<480;y++)
{
DrawPixel(screen, x,y,y/2,y/2,x/3);
}
}
Sulock(screen);
SDL_Flip(screen);
}
Also, if you get an error about undefined reference to WinMain@16 in Dev-C++, I use this hack:
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32) && defined(__MINGW32__) //hack
#define SDL_main WinMain
#endif
#include "SDL/SDL.h"
RubensWith i am starting in SDL this tutorial is a wonderfull
Congratulations !!!!!!
hthe reason why people are getting "undefined reference to WinMain@16" in Dev-C++ is probably because you need to compile the project as a console. not win32.
sfx1999Actually, it is caused because SDL redefines the main function as SDL_main. So, you end up having no main().
Add your comment: