2012-02-02 71 views
-1

我需要你的帮助。整天我试图找出为什么我得到这个error,但没有出现在我的脑海里。我希望我的程序要做的事情是拍摄一张png图像并将其切片成瓷砖,以便稍后用于制作地图。 那么,任何人都可以帮助我解决这个错误?C++和SDL-访问冲突错误

#include "SDL.h" 
#include "SDL_image.h" 
#pragma comment(lib, "SDL.lib") 
#pragma comment(lib, "SDLmain.lib") 
#pragma comment(lib, "SDL_image.lib") 

    int main(int argc,char *argv[]) { 


    SDL_Init(SDL_INIT_EVERYTHING); 


    SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); 

    IMG_Init(IMG_INIT_PNG); 
    SDL_Surface *mapTileSet; 
    mapTileSet = IMG_Load("map.png"); 

    SDL_Surface *myTiles[4]; 

    for(int i = 0; i < 3; i++) 
     myTiles[i] = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCCOLORKEY, 32, 32, 32, 0, 0, 0, 0); 
    for(int y = 0; y < 3; y++) 
    { 
     for(int x = 0; x < 3; x++) 
     { 
      int slice_x = x * 32;  
      int slice_y = y * 32;  

      SDL_Rect srcRect;  
      srcRect.x = slice_x;  
      srcRect.y = slice_y;  
      srcRect.w = 32; 
      srcRect.h = 32;  
      SDL_Rect dstRect;  
      dstRect.x = 0;  
      dstRect.y = 0; 

      int i = x + y * 4; 
      SDL_BlitSurface(mapTileSet, &srcRect, myTiles[i], &dstRect); 
     } 
    } 

    SDL_BlitSurface(myTiles[0], 0, screen, 0); 
    SDL_Flip(screen); 
    SDL_Delay(10000); 


    IMG_Quit(); 

    SDL_Quit(); 

    return 0; 
} 

调试日志:

'project.exe': Loaded 'C:\Users\Rokas\Desktop\The Game\The Game\Debug\project.exe', Symbols loaded. 
'project.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Program Files\AVAST Software\Avast\snxhk.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Users\Rokas\Desktop\The Game\The Game\SDL.dll', Binary was not built with debug information. 
'project.exe': Loaded 'C:\Windows\System32\advapi32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\sechost.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\gdi32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\user32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\lpk.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\usp10.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\winmm.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Users\Rokas\Desktop\The Game\The Game\SDL_image.dll', Binary was not built with debug information. 
'project.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded. 
'project.exe': Loaded 'C:\Windows\System32\imm32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\msctf.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\ddraw.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\dciman32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\setupapi.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\cfgmgr32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\oleaut32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\ole32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\devobj.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\dwmapi.dll', Cannot find or open the PDB file 
'project.exe': Unloaded 'C:\Windows\System32\ddraw.dll' 
'project.exe': Unloaded 'C:\Windows\System32\dwmapi.dll' 
'project.exe': Unloaded 'C:\Windows\System32\setupapi.dll' 
'project.exe': Unloaded 'C:\Windows\System32\devobj.dll' 
'project.exe': Unloaded 'C:\Windows\System32\oleaut32.dll' 
'project.exe': Unloaded 'C:\Windows\System32\ole32.dll' 
'project.exe': Unloaded 'C:\Windows\System32\cfgmgr32.dll' 
'project.exe': Unloaded 'C:\Windows\System32\dciman32.dll' 
'project.exe': Loaded 'C:\Windows\System32\uxtheme.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\dwmapi.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\KBDUS.DLL', Cannot find or open the PDB file 
'project.exe': Unloaded 'C:\Windows\System32\KBDUS.DLL' 
'project.exe': Loaded 'C:\Windows\System32\KBDUS.DLL', Cannot find or open the PDB file 
'project.exe': Unloaded 'C:\Windows\System32\KBDUS.DLL' 
'project.exe': Loaded 'C:\Windows\System32\dsound.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\ole32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\powrprof.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\setupapi.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\cfgmgr32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\oleaut32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\devobj.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\dinput.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\hid.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\wintrust.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\crypt32.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\msasn1.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Windows\System32\cryptbase.dll', Cannot find or open the PDB file 
'project.exe': Loaded 'C:\Users\Rokas\Desktop\The Game\The Game\libpng15-15.dll', Binary was not built with debug information. 
'project.exe': Loaded 'C:\Users\Rokas\Desktop\The Game\The Game\zlib1.dll', Binary was not built with debug information. 
First-chance exception at 0x681247e3 in project.exe: 0xC0000005: Access violation reading location 0xccccccf8. 
Unhandled exception at 0x681247e3 in project.exe: 0xC0000005: Access violation reading location 0xccccccf8. 
The thread 'Win32 Thread' (0xd94) has exited with code -805306369 (0xcfffffff). 
The thread 'Win32 Thread' (0x12f8) has exited with code -805306369 (0xcfffffff). 
The program '[3568] project.exe: Native' has exited with code -805306369 (0xcfffffff). 
+0

用'SDL_Init'设置断点,按F5开始调试,然后按F10/F11直到找到导致崩溃的行。当你发现它时,你可能会意识到发生了什么,以及你为什么访问你不应该访问的内存。 – LihO 2012-02-02 18:51:10

+0

所以你的问题解决了? – LihO 2012-02-02 19:03:11

+0

对不起,我在那篇文章中的错误。当我编辑行 SDL_BlitSurface(mapTileSet,&srcRect,myTiles [i],&dstRect); } 并用0替换myTiles [i]索引,它显示我需要的内容。所以现在我想我知道,问题在哪里。谢谢你的诋毁建议。 – Rokas 2012-02-02 19:05:20

回答

0

你正在创建砖这样的:

for(int i = 0; i < 3; i++) 
    myTiles[i] = SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCCOLORKEY, 32, 32, 32, 0, 0, 0, 0); 

在哪里我将等于0,1,2

但访问他们喜欢这个:

int i = x + y * 4; 
    SDL_BlitSurface(mapTileSet, &srcRect, myTiles[i], &dstRect); 

其中x和y分别为0,1和2.一旦y大于0,您将访问无效数据。

通常,对于包含循环,i = x + y * 4语句似乎格式不正确,因为它将生成序列{0,1,2,4,5,6,8,9,10},其中省略元素3和7.

+0

是的,我也看到了这个错误,但是谢谢。问题解决了。 – Rokas 2012-02-02 19:50:35