help me with sdl
k, this compiles and links without error or warning but when I go to open the exe. the sdl window pops up and it instantly freeze's, I get a window's error to send a error report, whatever, it closes....
any help will help
#include <math.h>
#include <stdlib.h>
#if defined(_MSC_VER)
#include "SDL.h"
#else
#include "SDL/SDL.h"
#endif
SDL_Surface *screen;
//***************************************************************************
#define PITCH (screen->pitch /4)
void init()
{
int i,j, pos;
for (i = 0; i < 640; i++)
{
int p = (int)((sin((i + 3247) * 0.02) * 0.3 * sin((i + 2347) * 0.04) * 0.1 + sin((i + 4378) * 0.01) * 0.6) * 100 + 380);
pos = p * PITCH + i;
for (j = p; j < 480; j++)
{
((unsigned int*)screen->pixels)[pos] = 0x007f00;
pos += PITCH;
}
}
}
//***************************************************************************
void newsnow()
{
int i;
for ( i = 0; i < 8; i++)
((unsigned int*)screen->pixels) [rand() % 638 + 1] = 0xffffff;
}
//***************************************************************************
void snowfall()
{
int i, j;
unsigned int *fb = (unsigned int*)screen->pixels;
for (j = 479; j >= 0; j--)
{
int ypos = j * PITCH;
for (i = 1; i < 639; i++)
{
if (fb[ypos + i] == 0xffffff)
{
if (fb[ypos + i + PITCH] == 0)
{
fb[ypos + i * PITCH] = 0xffffff;
fb[ypos + i] = 0;
}
}
}
}
}
//***************************************************************************
void render()
{
// Lock surface if needed
if (SDL_MUSTLOCK(screen))
if (SDL_LockSurface(screen) < 0)
return;
// ask SDL for the time in milliseconds
int tick = SDL_GetTicks();
newsnow();
snowfall();
// Unlock if needed
if (SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);
// tell SDL to update the whole screen
SDL_UpdateRect(screen, 0, 0, 640, 480);
}
// entry point
int main(int argc, char *argv[])
{
// initialize SDL's subsystems - in this case only vidio
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
// register SDL_Quit to be called at exit; make sure things are
// cleaned up when we quit
atexit (SDL_Quit);
// attempt to create a 640 by480 window with 32bit pixels
screen = SDL_SetVideoMode (640, 480, 32, SDL_SWSURFACE);
// if we fail, return error
if ( screen == NULL )
{
fprintf(stderr, "Unable to set 640x480 vidio %s\n", SDL_GetError());
exit(1);
}
init();
// main loop: loop forever
while (1)
{
// render stuff
render();
// poll for events, and handle the ones we care about
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
break;
case SDL_KEYUP:
// if escape is pressed, return ( and thus quit )
if (event.key.keysym.sym == SDLK_ESCAPE)
return 0;
break;
case SDL_QUIT:
return(0);
}
}
}
return 0;
First off for posting large amounts of code use [ source ] code [ /source ] tags without the spaces.
Regarding your problem try running your program through the debugger, if you are using Visual Studio/Visual C++ the debugger will point to the exact line on which the error but my guess is on memory access violations on the screen buffer. Some of those screen-pixels[foo] calculations look extremely iffy to me.
Regarding your problem try running your program through the debugger, if you are using Visual Studio/Visual C++ the debugger will point to the exact line on which the error but my guess is on memory access violations on the screen buffer. Some of those screen-pixels[foo] calculations look extremely iffy to me.
Is your unsigned int 16 or 32 bits (you are sticking 3 byte values into the pixel colors).
Is your screen->pitch in bytes (so your are dividing by 4 to get a 32bit
equivalent to use) ??
When you are accessing the surface directly with screen->pixels you are asking for trouble. I would place SDL_Event event; outside that while loop, but I don't know if it matters. Probably not.
Quote:Original post by VosKeyGen
k, this compiles and links without error or warning but when I go to open the exe. the sdl window pops up and it instantly freeze's, I get a window's error to send a error report, whatever, it closes....
any help will help
Well what you are doing is extremly dangerous just modifying the screen like that. What you need to do is take a look at this tutorial to see how to properly work with writing pixels to the screen. Use that and you should be able to get the effects you want. Good luck!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement