LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2003, 06:32 PM   #1
Cruelpeace
LQ Newbie
 
Registered: Jan 2003
Location: Orlando, Florida
Distribution: MacOS X and Slackware
Posts: 13

Rep: Reputation: 0
Question using SDL with C


Okay, I've been working with SDL, trying to figure out if I can get something to work out of it rather than an empty window. So I went to libsdl.org and found a tutorial, blah blah blah..... Now I went through the program using the tutorial, learning and trying what's going on step by step. (actually, two tutorials that sort of seamed into one) Anyways, at the last part the program setup two 'for' loops that would draw pixels, move them around, and change their colors as they went. Unfortuntaely I get this weird error:

'for' loop initial declaration used outside C99 mode

The problem lies in the DrawScene Function. It comes up for both 'for's (one for x one for y). I'm running Appbuilder in MacOSX and the only difference from it running and it not is this little loop right here. Thanks ahead for any help.

--------- Source Code Posted Below, Copied from Cone3D Programming -------

#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;
}
 
Old 02-04-2003, 07:44 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
without knowing appbuilder I'd say try to use
standard C-mode instead of C99 :)

Cheers,
Tink
 
Old 02-05-2003, 12:00 PM   #3
Cruelpeace
LQ Newbie
 
Registered: Jan 2003
Location: Orlando, Florida
Distribution: MacOS X and Slackware
Posts: 13

Original Poster
Rep: Reputation: 0
Do you know how to do that in any other IDEs? App Builder/Project Builder uses the PPC OS-X port of GCC so I don't know why it would be trying to compile it in c99 form anyways. Thanks though.
 
Old 02-06-2003, 12:59 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Sorry mate, in my gcc implementation (2.95.3) there's
no mentioning of C99 :\

Cheers,
Tink
 
Old 07-19-2003, 05:57 AM   #5
WillSams
LQ Newbie
 
Registered: Jul 2003
Posts: 1

Rep: Reputation: 0
Don't you just love ANSI? The problem is the variables you declared in your for loops. In c99, that's not authorized :/ Just re-write your function:

void DrawScene(SDL_Surface *screen)
{
int x,y; //declared outisde of for loop because of c99

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

I found this topic looking for the answer myself I decided to register to let you know what I found.

Last edited by WillSams; 07-21-2003 at 05:32 PM.
 
Old 07-21-2003, 04:04 PM   #6
jspenguin
Member
 
Registered: Feb 2003
Location: Wichita, KS
Distribution: Heavily modified Redhat
Posts: 194

Rep: Reputation: 30
Somewhat off topic: This type of construct IS valid in C++.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
busybox framebuffer/SDL question. jfbertrand Linux From Scratch 0 09-09-2005 02:17 AM
Trying to program an SDL application but cannot find the SDL.h file:SuSE 9.2&KDevelop pujolasdf Linux - Newbie 4 03-13-2005 07:50 AM
question on NeHe OpenGL/SDL/linux tutorial Moebius Programming 0 12-08-2004 10:05 PM
Question about SDL OpenGL and -lglut doody Programming 2 11-16-2004 04:17 PM
SDL Help dtheorem Linux - Software 1 11-04-2004 04:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration