Advertisement

[Question] map algorithm in sdl is flickerin'

Started by July 31, 2015 02:16 AM
5 comments, last by Servant of the Lord 9 years, 5 months ago

hey everyone here !!! wishing everything is great for everyone !!!

I'm a newbie here and i don't know if this correct but i really to learn how to do something , im trying lo load a map in a screen , and the thing is the map load correctly but every time..i load another image the screen is flickering in addition when I Get full screen is like a chess board.. I would like a litle help


int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_Window *window = SDL_CreateWindow("titu", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, S_W,S_H, SDL_WINDOW_SHOWN/*SDL_WINDOW_FULLSCREEN*/);
	SDL_Renderer *render = SDL_CreateRenderer(window, -1 , SDL_RENDERER_ACCELERATED |SDL_RENDERER_PRESENTVSYNC);
    SDL_Event ev;
    bool salir = true;
    SDL_Rect rect={0,0,32,32};

    int arreglo[MAX_ROW][MAX_COL]={
    	/*   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23        */
    		{1,1,1,1,1,1,1,1,1,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,2},
			{2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2}
    };
    for(int i= 0; i < MAX_COL ; i++)
    {
    	///cout <<endl;
      for(int j=0; j < MAX_ROW;j++){
    	if(arreglo[j][i] == 2) ////  correcto.
    	   rect.x  =32;
     else if(arreglo[j][i]==1)
    	   rect.x = 0;
     else if(arreglo[j][i]==3)
         	   rect.x = 64;
       ///cout << arreglo[j][i]<<',';
     DrawMap(render, &rect, i*32,j*32,32); ///copy the render in the screen.
     SDL_RenderPresent(render);
      }
    }

obviously i have a game loop where I make a litle delay using : 1000/fps -(startime-lastime)

i dont put it because the whole code is at home ... but the idea is clear , is what i believe..

thank you .

obviously i have a game loop where I make a litle delay using : 1000/fps -(startime-lastime)


I don't see that anywhere in your code. If there is flickering, that may be an important piece of code we need to see.

Nor do I see your DrawMap() code. That's also an important part of this puzzle.

i dont put it because the whole code is at home ... but the idea is clear , is what i believe..


Programming doesn't work that way.
You may have an "idea" that you think is how you wrote your code, but the compiler doesn't know what you intended, it only knows what you wrote.

Code is code. If there is a problem in your code, we need to see your code to see the problem. Unrelated similar code doesn't cut it. We need to see the actual code, to see the actual problem.

Advertisement

While full code would be better, looks to me like your calling SDL_RenderPresent once per tile. You should only present your scene after you have rendered the entire thing, once per "frame". Also with 3D acceleration, a present call is also likely to invalidate the back buffer contents, meaning you have to redraw the entire screen, not just add/edit parts, allthough cant remember if this applies to SDL or not.


    for(int i= 0; i < MAX_COL ; i++)
    {
    	///cout <<endl;
        for(int j=0; j < MAX_ROW;j++){
    	    if(arreglo[j][i] == 2) ////  correcto.
    	         rect.x  =32;
            else if(arreglo[j][i]==1)
    	        rect.x = 0;
            else if(arreglo[j][i]==3)
         	rect.x = 64;
            ///cout << arreglo[j][i]<<',';
           DrawMap(render, &rect, i*32,j*32,32); ///copy the render in the screen.
           SDL_RenderPresent(render); //SyncViews: Call this once per frame, after everything has been rendered/drawn
        }
    }

Servant of the lord I agreed with you !!! right now im going to put the all the code...

SyncView after I Read your answer I realized that I Was in a big o giant mistake I Was showing every single tile since I load ,this is a mistake.. thank to all ...

bellow im going to put the code..



#include<SDL2\SDL.h>
#include<iostream>
using namespace std;


#define S_W 		400
#define S_H 		400
#define MAX_ROW  	14
#define MAX_COL 	23
#define UNUSED	    0X32
void DrawMap(SDL_Renderer *render , SDL_Rect *rect, int x,int y,int size)
{
	SDL_Rect offset;
	offset.x  = x;
	offset.y  = y;
	offset.h  = 32;
	offset.w  =	32;
	SDL_Texture *text = SDL_CreateTextureFromSurface(render,SDL_LoadBMP("tiles.bmp"));
	if(text == NULL)
	{
		cout <<"Unnable to load a map!!"<<endl;
	}
	else
	{
	   SDL_RenderCopy(render, text, rect, &offset);
	}

}

int main(int argc, char *argv[])
{
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_Window *window = SDL_CreateWindow("titu", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, S_W,S_H, SDL_WINDOW_SHOWN|SDL_WINDOW_FULLSCREEN);
	SDL_Renderer *render = SDL_CreateRenderer(window, -1 , SDL_RENDERER_ACCELERATED |SDL_RENDERER_PRESENTVSYNC);
    SDL_Event ev;
    bool salir = true;
    SDL_Rect rect={0,0,32,32};

    int arreglo[MAX_ROW][MAX_COL]={
    	/*   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23        */
    		{2,2,2,2,2,1,1,1,1,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ,2},
			{2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2},
			{2,2,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2}
    };
    for(int i= 0; i < MAX_COL ; i++)
    {
    	///cout <<endl;
      for(int j=0; j < MAX_ROW;j++){
    	if(arreglo[j][i] == 2) ////  correcto.
    	   rect.x  =32;
     else if(arreglo[j][i]==1)
    	   rect.x = 0;
     else if(arreglo[j][i]==3)
         	   rect.x = 64;
       ///cout << arreglo[j][i]<<',';
     DrawMap(render, &rect, i*32,j*32,32); ///copy the render in the screen.

      }
    }
    SDL_RenderPresent(render);


    while(salir)
    {
       	while(SDL_PollEvent(&ev))
       	{
       		if(ev.type == SDL_QUIT || ev.type == SDL_KEYDOWN)
       		{
       			salir = false;
       		}

       	}

    }

Also note that you are loading a new BMP for every tile (SDL_LoadBMP()) and never calling SDL_FreeSurface(). You are going to quickly run out of memory if you do that.

You should only call SDL_LoadBMP() once per bitmap file, and then you can create textures using a reference to that surface you created.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

OK , im going to add as parameter, thank you :)

Advertisement

Is your problem fixed now, or is something still wrong?

This topic is closed to new replies.

Advertisement