Pelles C forum
C language => Graphics programming => Topic started by: net2011 on December 20, 2011, 08:21:01 AM
-
Hello everyone. I copied this program from a website, sorry don't know where, and when I compile the following code with #include<SDL/SDL.h> it gives me fatal error #1035: Can't find include file <SDL/SDL.h> and when I compile it with #include<SDL.h> it gives me error #2120: Redeclaration of 'SDL_main', previously declared at C:\Program Files\PellesC\Include\SDL_main.h(57); expected 'int __cdecl function(int, char * *)' but found 'int __cdecl function(void)'
okay here is the code#include <SDL/SDL.h>
#include <stdlib.h>
#include <math.h>
#define BALL_SIZE 256
#define TEX_SIZE 1024U
#define NUM_BALLS 8
#define NUM_STARS 256
struct point_t {
int x;
int y;
};
static void balls(SDL_Surface *s, unsigned int frames);
static void rotozoom(SDL_Surface *s, unsigned int frames);
static void stars(SDL_Surface *s);
int main(void) {
srand(0xDEADBEEF);
SDL_Surface *screen;
screen = SDL_SetVideoMode(640,480,32, SDL_SWSURFACE);
balls(screen, 850);
rotozoom(screen, 850);
}
static void balls(SDL_Surface *s, unsigned int frames) {
/* Metaballs a.k.a. blobs. The algorithm:
Loop through every ball:
Go through the light map x,y:
if lightmap x,y + ball x,y is under the limits, we add(w/ += operator) to screen x,y(ball position + lightmap x,y)
the value in lightmap x,y.
if the resulting value is > 255, then we actually just let it overflow. This creates the nice effect inside the ball.
*/
unsigned int h = BALL_SIZE/2; /* x,y of the center of the ball */
unsigned int px,py;
unsigned int x=0;
unsigned int y=0;
unsigned int i=0;
unsigned int *pp;
unsigned int f=0;
unsigned int p;
double sf = 0.0f; /* variable for fade-in/-out effect. */
/* Static arrays to save some space */
static unsigned int map[BALL_SIZE*BALL_SIZE];
static struct point_t ball[NUM_BALLS];
pp = (unsigned int*)s->pixels; /* "Pixel Pointer" to point to the surface pixels */
/* Generate the lightmap */
for(y=0;y<BALL_SIZE;y++) {
for(x=0;x<BALL_SIZE;x++) {
/* So: in the lightmap the strenght is defined by 1/(dist_from_center ^ 4) * size * 2^20(1 048 576) */
p = 1/( pow(sqrt((x-h)*(x-h)+(y-h)*(y-h)),4) +1 )*(BALL_SIZE<<20);
if(p>255) p = 255; /* Let's make sure the strenght won't overflow */
map[y*BALL_SIZE+x] = p;
}
}
/* Randomize the ball positions */
for(i=0;i<NUM_BALLS;i++) {
ball[i].x = BALL_SIZE+rand()%(640-(BALL_SIZE*2));
ball[i].y = BALL_SIZE+rand()%(480-(BALL_SIZE*2));
}
/* Let's loop until we've drawn enough frames */
while(f<frames) {
/* Fade in effect */
if(sf < 1.0 && f < 50) sf+=0.05;
/* Call the starfield */
stars(s);
/* Loop through every ball */
for(i=0;i<NUM_BALLS;i++) {
for(x=1;x<BALL_SIZE;x++) {
for(y=1;y<BALL_SIZE;y++) {
/* If and only if the x,y is inside the box .. */
if((y+ball[i].y) < 400 && y+ball[i].y > 80 && x+ball[i].x < 520 && x+ball[i].x > 120)
/* ..draw the pixel. The color is retrieved from the lightmap x,y. sf is the fade in/out multiplier. */
pp[(int)((y+ball[i].y)*640+(x+ball[i].x))] += (map[y*BALL_SIZE+x]*sf);
}
}
/* Let's make the balls float around */
px = (320-BALL_SIZE/2+(cos(f/18.6)*160*sf)) + sin(f/18.4+i)*80*sf;
py = (240-BALL_SIZE/2-(sin(f/21.4)*125*sf)) - cos(f/24.5+i)*60*sf;
ball[i].x = px;
ball[i].y = py;
}
/* Fade out effect */
if(frames-f < 150 && sf>0.012) sf-=0.0125;
/* Let's make sure we don't run too fast */
SDL_Delay(10);
SDL_Flip(s);
/* increase the framecount */
f++;
}
/* We're done. */
return;
}
static void stars(SDL_Surface *s) {
/*
This function handles the drawing and rotation of the background starfield, also draws the rectangle.
*/
static unsigned int i,x,y;
static unsigned int *pp;
static double v;
static struct point_t stars[NUM_STARS];
static unsigned int init;
/* If the stars haven't been initalized yet, then do it now */
if(!init) {
init=1;
for(i=0;i<NUM_STARS;i++) {
stars[i].x = -250+rand()%(250+640);
stars[i].y = -250+rand()%(250+480);
}
}
/* Increase the angle of the starfield */
v += 0.0075;
pp = (unsigned int*)s->pixels;
/* Let's clear the screen left out from the box, upper and lower segments */
memset(pp,0,80*640*4);
memset(pp+401*640,0,79*640*4);
/* And the same to the segments left to the sides of the box */
for(i=80;i<401;i++) {
memset(pp+(i*640),0,120*4);
memset(pp+(i*640+520), 0, 120*4);
}
for(i=0;i<NUM_STARS;i++) {
x = 320 + (stars[i].x*cos(v)+stars[i].y*sin(v));
y = 240 + (stars[i].y*cos(v)-stars[i].x*sin(v));
if(x>0 && x < 640 && y > 0 && y < 480) pp[y*640+x] = -1;
}
/* Let's draw the white rectangle. First the upper and lower borders. */
memset(pp+80*640+120, -1, 401*4);
memset(pp+400*640+120, -1, 401*4);
for(i=81;i<400;i++) {
/* Let's erase the stars inside the box */
memset(pp+i*640+121, 0, 400*4);
/* And draw the left and the right border of the box */
pp[i*640+120] = -1;
pp[i*640+520] = -1;
}
}
static void rotozoom(SDL_Surface *s, unsigned int frames) {
/*
At first this was supposed to be a rotozoomer with dynamic texture where the texture was created with a plasma effect...
Now, the effect is just a classical plasma effect where a texture is being applied on top of another texture and the two
textures are being rotated and moved around relative to each other. The speciality of this effect is that the textures are
being fit together with bitwise operators. The disorted image is achieved by using the same operators when creating the texture.
*/
unsigned int x, y, *pp, f=0,t=0;
/* Static to save some space */
static unsigned int col[512];
static unsigned int tex[TEX_SIZE*TEX_SIZE];
unsigned int x1, y1,x2,y2,fco,c;
unsigned char r,g,b;
double z=0.0, sf=0.0, tcos, tsin;
pp = (unsigned int*)s->pixels;
/* Generate random color palette */
for(x=0;x<512;x++) {
r = 192+sin(x/46.4)*95;
g = 64+cos(x/85.4)*48;
b = 16+sin(x/14.4)*4;
col[x] = r << 16 | g << 8 | b;
}
for(x=0;x<TEX_SIZE-1;x++) {
for(y=0;y<TEX_SIZE-1;y++) {
t = 128 + cos(x/37.9)*64 + sin(y/30.3)*64 - cos(x/35.3)*64 + sin((x&y)/4)*8; /* x^y brings in a small flavor of traditional XOR texture. */
tex[y*TEX_SIZE+x] = t;
}
}
while(f<frames) {
if(sf < 1.0 && f < 50) sf+=0.015;
z+=0.015;
stars(s);
/* Variable to make the effect more dynamic. */
fco = cos(f/16.4)*8 + sin(f/6.5)*12;
/* Let's precalculate these so we don't have to do that per-pixel */
tcos = cos(z);
tsin = sin(z);
x1 = 64+tcos*16 + tsin*12;
y1 = 64+tcos*16 + tsin*24;
/* Fade out effect */
if(frames-f < 75) sf/=1.075;
for(x=1;x<400;x++) {
for(y=1;y<320;y++) {
x2 = 560+(tcos*x+tsin*(y/2)); /* Rotations */
y2 = 560+(tcos*y-tsin*(x/2));
/* Calculate the color by adding two textures together by using bitwise AND operator. */
c = (col[tex[(x1+x)*TEX_SIZE+(y1+y)] + (tex[x2*TEX_SIZE+y2] & tex[y2*TEX_SIZE+x2])-fco]);
/* Let's break the color to it's RGB components. */
r = c >> 16 & 0xFF; g = c >> 8 & 0xFF, b = c & 0xFF;
/* Multiply the color components by the fade in/out multiplier and fuse them together */
pp[(y+80)*640+(x+120)] = (int)(sf*r) << 16 | (int)(sf*g) << 8 | (int)(sf*b);
}
}
/* Increase the framecount and slow down */
f++;
SDL_Delay(10);
SDL_Flip(s);
}
return;
}
Also, when I tried running the code with #include<SDL/SDL.h> it gave me error #2009: Conflicting argument declarations for function 'SDL_main'. and error #2048: Undeclared identifier 'DETECT'. When I ran it with #include<SDL.h> it gave me the same errors. Can anyone tell me what the fix for this is?
I really just want to make sure that I have all of the files necessary to do graphics, games and everything else you can do with sdl. Also, is there anything out there better for Pelles C inparticular than sdl? Thanks for the help.
-
Hallo,
do you have SDL installed?
czerny
-
Yes, I do have SDL installed actually. Here is a program that worked just fine but now all of a sudden it too is giving me the same exact errors (2009 and 2048) as the program above.
#include <stdio.h>
#include <SDL.h>
#define WIDTH 640
#define HEIGHT 480
#define BPP 4
#define DEPTH 32
void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint32 *pixmem32;
Uint32 colour;
colour = SDL_MapRGB( screen->format, r, g, b );
pixmem32 = (Uint32*) screen->pixels + y + x;
*pixmem32 = colour;
}
void DrawScreen(SDL_Surface* screen, int h)
{
int x, y, ytimesw;
if(SDL_MUSTLOCK(screen))
{
if(SDL_LockSurface(screen) < 0) return;
}
for(y = 0; y < screen->h; y++ )
{
ytimesw = y*screen->pitch/BPP;
for( x = 0; x < screen->w; x++ )
{
setpixel(screen, x, ytimesw, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
}
}
if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
SDL_Flip(screen);
}
int main(int argc, char* argv[])
{
SDL_Surface *screen;
SDL_Event event;
int keypress = 0;
int h=0;
if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_FULLSCREEN|SDL_HWSURFACE)))
{
SDL_Quit();
return 1;
}
while(!keypress)
{
DrawScreen(screen,h++);
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
keypress = 1;
break;
case SDL_KEYDOWN:
keypress = 1;
break;
}
}
}
SDL_Quit();
return 0;
}
Is there something wrong with my SDL all of a sudden?
-
Do you have your project paths set to include the SDL libs and headers?
-
Yes, I do. I when I installed SDL I followed the instructions on friedspace.com to the t. I also forgot to mention something about the second program that I posted, which is called graphics_program.c. When I compile the program it gives me "Use <stdlib.h> instead of non-standard <malloc.h>".
-
Ok so ... use stdlib instead... no problem.
-
Okay, could you please tell me how to do that?
-
I'm assuming that for some reason it is using malloc.h automatically because I didn't include malloc.h. Also, I searched my whole computer for stdlib.h and it was no where to be found. I have no idea what happened.
-
Oh, and another thing is I don't know where to go to get stdlib.h. Will I have to re-download SDL?
-
stdlib.h has nothing to do with SDL. It is one of the standard C librarys. You should learn something about the standard include files.
May be SDL is using malloc.h. So you can ignore this warning. Has nothing to do with your problem.
czerny
-
Okay, could you please tell me how to do that?
#include <stdlib.h>
-
Oh, and another thing is I don't know where to go to get stdlib.h. Will I have to re-download SDL?
It should be in your Pelles C install directory which is usually c:\program files\PellesC\include
If it's not there, your best bet is to uninstall Pelles C, delete the folders from program files and reinstall a clean copy.
If you followed the directions on friedspace.com you've put SDL right into your Pelles C distribution folders and that's a very bad idea. Make a separate folder in your project space for SDL and it's files... put them there and on a Project by Project basis, add those folders to your includes and libs... Only in the projects that need them. Don't do it as a global installation, it's likely to crash Pelles C as there is a very real risk of name collissions. (As would appear to be the case here)
Moreover... you don't actually need SDL to do graphics or games...Windows API, which uses files and libs already included in Pelles C, gives you practically everything you need...
Have a read of this.... http://www.winprog.org/tutorial/ (http://www.winprog.org/tutorial/)
-
I will definitely have a read of that. I tried including stdlib.h and it gave me all of the exact same errors listed above. It never did this before. Do you know what could've happened to cause these errors? Oh, by the way, I found stdlib.h in that include folder, so it's there.
-
your code
int main(void) {
srand(0xDEADBEEF);
SDL_Surface *screen;
screen = SDL_SetVideoMode(640,480,32, SDL_SWSURFACE);
balls(screen, 850);
rotozoom(screen, 850);
}
should be changed to
int main(int argc, char **argv) {
srand(0xDEADBEEF);
SDL_Surface *screen;
screen = SDL_SetVideoMode(640,480,32, SDL_SWSURFACE);
balls(screen, 850);
rotozoom(screen, 850);
return 0;
}
czerny
-
Well czerny, it looks like the code you suggested fixed one problem but now both programs are giving me the exact same error messages posted above. Once again, when I compile it gives me "Use <stdlib.h> instead of non-standard <malloc.h>" and when I try to run them they both give me "error #2009: Conflicting argument declarations for function 'SDL_main'." and error #2048: Undeclared identifier 'DETECT'.
-
I took MirekCZ's advice on this page here http://forum.pellesc.de/index.php?topic=2858.0 and it got rid of the "Use <stdlib.h> instead of non-standard <malloc.h>" message but it they still give me the same two errors.
-
What project settings have you used to produce sdlmain.lib?
czerny
-
In project options under compiler I have warnings set to level 1, calling conv set to _cdecl, machine is set to x86, inlining is set to default, the enable microsoft extensions box is checked under options, under assembler debug information is set to none, calling conv is set to cdecl, machine is set to x86, under linker these are all my library and object files kernel32.lib user32.lib gdi32.lib comctl32.lib comdlg32.lib advapi32.lib delayimp.lib sdl.lib sdlmain.lib, type is set to console under subsystem and machine is set to x86. I hope I understood your question. Thanks for the help.
-
There shouldn't be a linker tab, because you are building a static library. And obviously you can not include libmain.lib if you are just building libmain.lib.
Try building this library from scratch.
czerny