Skip to main content
gamedev.net
🔒 Locked

Pointer problems?

Started by TFS_Waldo Apr 9, 2006 at 1:12 PM 5 replies 600+ views
Original Post
TFS_Waldo
TFS_Waldo
Well, I am working on my 2D game engine using SDL. I current have a class for the engine itself, the app. window, graphics class, simple sprite class, and a renderer. In the graphics class I have functions to perform all graphical operations, of course. Well, the "CEngineGfx::Render" function calls "m_pRenderer->Render", and "m_pRenderer" is "CRenderer *m_pRenderer;". Well, when I call "g_pGfx->Render" (defined as "CEngineGfx *g_pGfx = new CEngineGfx;"), I get an access violation. When I debug, it points to this code in 'CEngineGfx::Render': if (m_pRenderer) // <-- This Line :) m_pRenderer->Render(); Even if I use the 'CRenderer' object directly, by defining 'CRenderer *g_pRenderer = new CRenderer;' and call 'g_pRenderer->Render()' I get the same error. And the debugger says the expression for 'g_pRenderer' cannot be evaluated. I call 'new' and everything. I just can't figure out what's wrong. I hope I explained clearly, and I hope it's enough info. Thanks in advance, Matt U.
:==- Why don't the voices just leave me alone?! -==:
CrazyCdn
CrazyCdn
Can you post some code please, I atleast find it easier to follow that way.

Thanks.
"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin
furiousuk
furiousuk
Try setting your pointer variable to NULL first and then assigning it to point at an object currently in scope. That could help.

Often times problems like this occur when the object pointed to goes out of scope (i.e. gets deleted).
TFS_Waldo
TFS_Waldo
Well, I'm not sure what happened. I went back and compiled/ran again, and it works just fine now. LoL.

But now I have another problem (not too related to this specific post):

My sprite class includes a "Draw" function, which just sets up a SDL_Rect, and calls "SDL_BlitSurface". Well, I add the sprite object to the renderer, and when I call "m_pGfx->Render()", and I move the sprite (using arrow keys), it never erases the old sprite. I mean, it draws the sprite at the new location. And I checked the "std::vector m_vecSprites;" object, and it only contains the one sprite each frame.

I call "m_pGfx->Render()" which loops through the sprite vector and calls "(*it)->Draw()", then I call SDL_Flip(screen); I can't figure out why it leaves the old sprite rect.
:==- Why don't the voices just leave me alone?! -==:
xEricx
xEricx
You need to clear your surface at each frame, probably in black... so just blit the entire surface with the black color, then draw your new surfaces.

You can also use a visible color, such as pink, when you'll be drawing the entire screen and you want to make sure every pixels get blit a valid value.

Hope this helps

Eric
Oluseyi
Oluseyi
You can save and restore a "saveunder" - the contents of the screen the object is about to be drawn over - for each frame, so you don't have to redraw the entire screen. This is one of the steps involved in a "dirty rect" compositing approach.

If you have a lot (this is relative to the size of sprites and the screen dimensions) of objects animating at different layers, occluding each other, then it's not worth it. Just redraw the entire screen.
TFS_Waldo
TFS_Waldo
Well, I tried "SDL_FillScreen(screen, NULL, 0x00000000);" and it didn't work. But I realized I had to put it at the beginning of the "Render" function -- now it's working! Thanks. =)

But why do I have to do that? In some of my old SDL applications, I had to fill the screen with black. But once I added certain things, it would cause a screen flicker. I think it happened when I would add a moving object or something. But I'll cross that bridge when I get to it.

Thanks again! =)

And adding a call to SDL_FillRect to clear the screen dropped my frame rate by about 8-10 frames per second. This is normal, right?

I'm on a GeForce FX 5200 128MB, PIII 733MHz. I figured it would cause some kind of frame rate drop. And it's not a major performance loss. And here's my "CalcFPS" function, is there anything wrong with it?

DWORD g_dwLastTime = 0;    // Previous value from 'timeGetTime()' functionunsigned int g_nFrames = 0;    // Frame countvoid CalcFPS(){    if ((timeGetTime() - g_dwLastTime) >= 1000)    {        // Calculate frames per second        float fFPS = ((timeGetTime() - g_dwLastTime) / 1000);        char szFPS[30];        // Print title and FPS to string        sprintf(szFPS, "Simple Engine Test :: FPS: %d", int(fFPS));        g_pEngine->SetAppTitle(szFPS);        g_dwLastTime = timeGetTime();        g_nFrames = 0;    }    g_nFrames++;}


And should I call 'g_nFrames++;' before or after the 'if' statement?

I think that's right. But I wanted to make sure, because I memorized the basic code for FPS and "transferred" it to my computer. =P

Thanks! =)
:==- Why don't the voices just leave me alone?! -==:

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.