• Advertisement

Archived

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

Problem with Cone3d's 1st SDL Tutorial

This topic is 5831 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

I have Dev-C++ and i got the SDL libs for it on cone3d''s site. So, I went through the first tutorial, tried to compile it, and BAM! it wont compile and gives me a ton of errors! I also had the same problem with the first tutorial here on gamedev. Any ideas? p.s. Yes I linked in the SDL libs, and yes, i am *pretty* sure I configured it properly. ------------------------------ If someone asks you "A penny for your thoughts" and you put your two cents in, do you get change?

Share this post


Link to post
Share on other sites
Advertisement
Try these steps instead. They might be different. I''m not sure.

------------------------------
Simple DirectMedia Layer:

Main Site - (www.libsdl.org)
Cone3D Tutorials- (cone3D.gamedev.net)
GameDev.net''s Tutorials - (Here)

OpenGL:

Main Site - (www.opengl.org)
NeHe Tutorials - (nehe.gamedev.net)
Online Books - (Red Book) (Blue Book)

Share this post


Link to post
Share on other sites
Nope, still doesn''t work. Here is the code if anyone can tell me what''s wrong:
  
#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;
}

As you can see, it is the code from the first cone3d tutorial.

------------------------------
If someone asks you "A penny for your thoughts" and you put your two cents in, do you get change?

Share this post


Link to post
Share on other sites
I too have had compile problems ... for some reason Dev-C++ and atexit() dont get along on my comp. Try taking it out and just putting the code in for everytime you quit.

"If patience is a virtue, and ignorance is bliss, you can have a pretty good life if you''re stupid and willing to wait"

Share this post


Link to post
Share on other sites
It still doesn''t work!!!!!! Here is a listing of the errors:

untitled1.c: In function `DrawScene'':
untitled1.c:74: parse error before `int''
untitled1.c:74: `x'' undeclared (first use in this function)
untitled1.c:74: (Each undeclared identifier is reported only once
untitled1.c:74: for each function it appears in.)
untitled1.c:74: parse error before `)''
untitled1.c:76: `y'' undeclared (first use in this function)
untitled1.c:76: parse error before `)''
untitled1.c: At top level:
untitled1.c:81: warning: parameter names (without types) in function declaration
untitled1.c:81: conflicting types for `Sulock''
untitled1.c:18: previous declaration of `Sulock''
untitled1.c:81: warning: data definition has no type or storage class
untitled1.c:82: warning: parameter names (without types) in function declaration
untitled1.c:82: warning: data definition has no type or storage class
untitled1.c:83: parse error before `}''
untitled1.c: In function `SDL_main'':
untitled1.c:94: parse error before `*''
untitled1.c:95: `screen'' undeclared (first use in this function)
untitled1.c:101: parse error before `int''
untitled1.c:103: `done'' undeclared (first use in this function)

I told you there were a lot of errors. Hopefully someone can help me based on this.

------------------------------
If someone asks you "A penny for your thoughts" and you put your two cents in, do you get change?

Share this post


Link to post
Share on other sites
I''m such an idiot. All I had to do was compile it for C++ instead of C! i don''t know why i made it C in the first place...
*GarlandIX smacks himself in the forehead*

------------------------------
If someone asks you "A penny for your thoughts" and you put your two cents in, do you get change?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why did that work?
I don''t see anything in your code that''s C++ only.

I thought SDL didn''t depend on C++.
It even says that SDL was written in C on their homepage.

Share this post


Link to post
Share on other sites
let''s for example look at DrawScene

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);
}


C doesn''t let you do for(int x=0;x<640;x++), you must declare your variables at the top of your program like

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


That code should compile in a C enviroment.


---
cone3d
http://cone3d.gamedev.net
My software never has any bugs - it just generates random features

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Really, not even in the newest standard(C99 I think)?

Anyway, I see what you''re saying, see I write in C but
still compile in C++, so things like that are very
transparent to me, especially since I don''t declare my
variables that way.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I looked it up, you can declare variables in a for loop
according to C99 standard, so either his compiler is very
compliant or it is following the old standard or something
else was amiss.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I looked it up, you can declare variables in a for loop

(in the initialization part like your example)

according to C99 standard, so either his compiler is not very
compliant or it is following the old standard or something
else was amiss.

Share this post


Link to post
Share on other sites
hmm. maybe, but I''m pretty sure that the errors

untitled1.c: In function `DrawScene'':
untitled1.c:74: parse error before `int''
untitled1.c:74: `x'' undeclared (first use in this function)
untitled1.c:74: (Each undeclared identifier is reported only once
untitled1.c:74: for each function it appears in.)
untitled1.c:74: parse error before `)''
untitled1.c:76: `y'' undeclared (first use in this function)
untitled1.c:76: parse error before `)''

refer to that for loop thingy. I remember reading from some c tutorial once that you had to manually declare your variables at the top of your programs in C. Maybe that has changed or the tutorial contained old information. I don''t really know.


---
cone3d
http://cone3d.gamedev.net
My software never has any bugs - it just generates random features

Share this post


Link to post
Share on other sites
I had the same kind of problem when porting DirectX, VisualC++ programs to g++ and SDL.
As Cone3d suggested, declare the variable controling the loop at the top of the function and not in the loop itself.
My version of gcc is quite recent, that''s the one included with Redhay7.2

Share this post


Link to post
Share on other sites

  • Advertisement