Public Group

[Question] map algorithm in sdl is flickerin'

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

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 on other sites

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;
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 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 on other sites

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

Share on other sites

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

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• Forum Statistics

• Total Topics
633334
• Total Posts
3011407
• Who's Online (See full list)

There are no registered users currently online

×