• Advertisement
Sign in to follow this  

[Question] map algorithm in sdl is flickerin'

This topic is 993 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 . 

Share this post


Link to post
Share on other sites
Advertisement

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;
       		}

       	}

    }

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement