I'm just starting to do SDL in C++, and I wrote a program to test it out, and basically it plots a big spray of particles on the screen. The problem I'm having is an external error in compilation saying int main() doesn't exist, when it's clearly there. Please help, because it's driving me nuts!
Here's my code:
----------------------------------------------------------
#include "SDL.h"
#include "SDL_keysym.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint32 color = SDL_MapRGB(screen->format, r,g,b);
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
struct Cstar
{
double vx, vy, x, y;
};
int main()
{
SDL_Surface *screen;
bool done= false;
SDL_Event event;
const MaxStars = 1000;
Cstar stars[MaxStars];
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if (!screen)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
int i;
double cx, cy;
i=0;
for (i=0; i<=MaxStars; i++)
{
stars.x = (double) (160 + (rand()%40)*((rand()%3)-1));
stars.y = (double) (rand()%20 + 1);
stars.vx = (double) (((rand()%3)-1)*((rand()%6)+1)*sin(rand()%360));
stars.vx = (double) (((rand()%3)-1)*((rand()%6)+1)*cos(rand()%360));
}
do
{
//particles
if(SDL_QuitRequested())
done = true;
SDL_LockSurface(screen);
i=0;
for (i=0; i<=MaxStars; i++)
{
beginupdate:
cx = stars.x + stars.vx;
cy = stars.y + stars.vy;
if(cx < 1 || cx > 638){
stars.vx = -stars.vx;
cx = stars.x + stars.vx;
}
if (cy < 1){
stars.vy = -stars.vy;
cy = stars.y + stars.vy;
}
if (cy > 478){
stars.x = (double) (160 + (rand()%40)*((rand()%3)-1));
stars.y = (double) (rand()%20 + 1);
stars.vx = (double) (((rand()%3)-1)*((rand()%6)+1)*sin(rand()%360));
stars.vx = (double) (((rand()%3)-1)*((rand()%6)+1)*cos(rand()%360));
goto beginupdate;
}
stars.x = cx;
stars.y = cy;
stars.vy = stars.vy + .28;
DrawPixel(screen, (int)cx, (int)cy, 255,255,255);
}
SDL_UnlockSurface(screen);
SDL_Flip(screen); //PUT (0, 0), buffer, PSET
if( SDL_PollEvent( &event ) ){
switch( event.type ){
case SDL_KEYDOWN:
switch(event.key.keysym.sym ){
case SDLK_ESCAPE:
done = true;
break;
default:
break;
}
default:
break;
}
}
}while (!done);
return 0;
}
-----------------------
Soon to come - a QBasic contest forum!
-----------------------
Here's my code:
----------------------------------------------------------
#include "SDL.h"
#include "SDL_keysym.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint32 color = SDL_MapRGB(screen->format, r,g,b);
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
struct Cstar
{
double vx, vy, x, y;
};
int main()
{
SDL_Surface *screen;
bool done= false;
SDL_Event event;
const MaxStars = 1000;
Cstar stars[MaxStars];
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if (!screen)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
return 1;
}
int i;
double cx, cy;
i=0;
for (i=0; i<=MaxStars; i++)
{
stars.x = (double) (160 + (rand()%40)*((rand()%3)-1));
stars.y = (double) (rand()%20 + 1);
stars.vx = (double) (((rand()%3)-1)*((rand()%6)+1)*sin(rand()%360));
stars.vx = (double) (((rand()%3)-1)*((rand()%6)+1)*cos(rand()%360));
}
do
{
//particles
if(SDL_QuitRequested())
done = true;
SDL_LockSurface(screen);
i=0;
for (i=0; i<=MaxStars; i++)
{
beginupdate:
cx = stars.x + stars.vx;
cy = stars.y + stars.vy;
if(cx < 1 || cx > 638){
stars.vx = -stars.vx;
cx = stars.x + stars.vx;
}
if (cy < 1){
stars.vy = -stars.vy;
cy = stars.y + stars.vy;
}
if (cy > 478){
stars.x = (double) (160 + (rand()%40)*((rand()%3)-1));
stars.y = (double) (rand()%20 + 1);
stars.vx = (double) (((rand()%3)-1)*((rand()%6)+1)*sin(rand()%360));
stars.vx = (double) (((rand()%3)-1)*((rand()%6)+1)*cos(rand()%360));
goto beginupdate;
}
stars.x = cx;
stars.y = cy;
stars.vy = stars.vy + .28;
DrawPixel(screen, (int)cx, (int)cy, 255,255,255);
}
SDL_UnlockSurface(screen);
SDL_Flip(screen); //PUT (0, 0), buffer, PSET
if( SDL_PollEvent( &event ) ){
switch( event.type ){
case SDL_KEYDOWN:
switch(event.key.keysym.sym ){
case SDLK_ESCAPE:
done = true;
break;
default:
break;
}
default:
break;
}
}
}while (!done);
return 0;
}
-----------------------
Soon to come - a QBasic contest forum!
-----------------------