Daniel <daniel_v at bigpond.com> wrote:
which OS are you using and which video card do you have? also,
could we see some more complete code to help us reproduce the bug
please?
This is all under win2k with an NVidia GeForce MX, a GeForce2 derivative.
I get a memory access violation as soon as the program runs.
When you single step it in the debugger where does it actually fail?
It says "First-chance exception at 0xC0000005: Access Violation. In the disassembler, if I go to 0xC0000005, all I see is ???. However, there is a yellow arrow on the line
“004013F8 movsx eax,byte ptr [edx]”
Which is inside _SDL_main, or at least, the source annotation shows _SDL_main several assembly lines above this.
In order to get more debugging info than “0xC00005: Access Violation”, I had to link against “Debugging Multithreaded DLL”, though the same crash with the same 0xC0000005 happens with “Multithreaded DLL”.
You’ll please forgive me if I haven’t provided enough info. I haven’t used MSVC’s debugger before, and I can’t seem to get it to step through my program line by line – instead it insists on stepping through each individual Assembly statement…
Here is the complete source of the code. The SetVideoMode call that crashes is active, while the one that just makes an obnoxious blank screen that I can’t break out of (even by using task manager to kill visual studio!) is commented out.
--------code follows
#include “SDL.h”
#include “SDL_endian.h” /* Used for the endian-dependent 24 bpp mode */
#include <stdlib.h>
static int RAND_MID = RAND_MAX / 2;
void DrawPixel(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B, int x, int y)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 ) {
return;
}
}
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;
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen, x, y, 1, 1);
}
short heads() { return abs(rand()) > RAND_MID ? 1 : 0; }
void updateColor(Uint8 *x) {
*x = (*x + (heads() == 1 ? 1 : 2)) % 200;
}
void move(int *x) {
static float inertia = 0.0;
inertia += (heads() == 1 ? 0.1F : - 0.05F);
if (inertia > 0.8F)
inertia = 0.3F;
*x = (inertia > 0.5F ? *x + 2 : *x - 2) % 400;
}
main(int argc, char *argv[])
{
printf(“Initilializing.\n”);
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, “Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_Surface *screen;
//Crashes:
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
/*Works, but can’t kill it:
screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN | SDL_SWSURFACE);
*/
if ( screen == NULL ) {
fprintf(stderr, “Unable to set 640x480 video: %s\n”, SDL_GetError());
exit(1);
}
printf(“Initilialization finished.\n”);
char *in = ‘\0’;
while (scanf("%c", *in) != ‘\n’);
/*int *x = 0, *y = 0;
Uint8 *r = (unsigned char *)0, *g = (unsigned char *)0, *b = (unsigned char *)200;
while (true) {
DrawPixel(screen, *r, *g, *b, *x, *y);
updateColor®;
updateColor(g);
updateColor(b);
move(x);
move(y);
}
*/
return 0;
}