Parallax backgrounds
This commit is contained in:
@@ -90,7 +90,6 @@ void player_proc(Entity e,int ft){
|
||||
|
||||
|
||||
// Scroll View
|
||||
//GameLib_MoveToPosH(e->pos,0.6f);
|
||||
GameLib_MoveToPos(e->pos,0.6f);
|
||||
}
|
||||
|
||||
|
||||
32
Game/main.c
32
Game/main.c
@@ -14,23 +14,33 @@ extern int gamelib_debug;
|
||||
|
||||
|
||||
DrawFnt font;
|
||||
DrawFnt font_shad;
|
||||
DrawImg imgBackground;
|
||||
|
||||
void MainGame_Text(int x, int y, char *text){
|
||||
Draw_SetColor(0.0f, 0.0f, 0.0f, 0.5f);
|
||||
Draw_DrawText(font, text, x+1, y+1);
|
||||
Draw_SetColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
Draw_DrawText(font, text, x, y);
|
||||
}
|
||||
|
||||
void ProcGame(){
|
||||
|
||||
}
|
||||
|
||||
void PostProcGame(){
|
||||
// Apply gravity to every entity
|
||||
GameLib_ForEachEnt(EntityApplyGravity);
|
||||
}
|
||||
|
||||
void PreDrawGame(float f){
|
||||
|
||||
}
|
||||
void DrawGame(float f){
|
||||
MainGame_Text(8,8,"Hello world!");
|
||||
}
|
||||
|
||||
int main(int argc,char *argv[]){
|
||||
int i,j;
|
||||
Entity *e;
|
||||
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
if (argc>1) {
|
||||
@@ -42,30 +52,26 @@ int main(int argc,char *argv[]){
|
||||
|
||||
GameLib_Init(640,480,"Game",20,60);
|
||||
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Load and initialize media.
|
||||
//
|
||||
font=Draw_DefaultFont(255,255,255,255);
|
||||
font_shad=Draw_DefaultFont(0,0,0,127);
|
||||
|
||||
imgBackground=Draw_LoadImage("data/background.png");
|
||||
Draw_SetOffset(imgBackground,0,0);
|
||||
GameEnts_Init();
|
||||
|
||||
|
||||
/////////////////////////
|
||||
// Initialize world.
|
||||
//
|
||||
GameLib_DelEnts();
|
||||
GameMap_LoadLevel("data/level_01.txt",64);
|
||||
|
||||
|
||||
/////////////////////////
|
||||
// Run the world.
|
||||
//
|
||||
|
||||
GameLib_Loop(ProcGame,PostProcGame,NULL,NULL);
|
||||
|
||||
GameLib_CleanParallaxBackgrounds();
|
||||
GameLib_AddParallaxBackground(imgBackground, (int[2]){512, 512}, (int[2]){0, 0}, (float[2]){0.5f, 0.0f});
|
||||
GameLib_Loop(ProcGame,PostProcGame,PreDrawGame,DrawGame);
|
||||
|
||||
return(0);
|
||||
}
|
||||
@@ -929,6 +929,35 @@ void Draw_DrawImgPartHoriz(DrawImg img,int x,int y,int w,int i){
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_ImgParallax
|
||||
//
|
||||
//
|
||||
void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2], int gamePos[2], int gameSize[2]){
|
||||
int paralaxPos[2];
|
||||
int mult[2];
|
||||
int x, y;
|
||||
|
||||
paralaxPos[0] = (gamePos[0] * parallaxFactor[0]) + imgOffset[0];
|
||||
paralaxPos[1] = (gamePos[1] * parallaxFactor[1]) + imgOffset[1];
|
||||
|
||||
mult[0] = floor(paralaxPos[0] / imgSize[0]);
|
||||
if(paralaxPos[0] < 0 ){ mult[0]--; }
|
||||
mult[1] = floor(paralaxPos[1] / imgSize[1]);
|
||||
if(paralaxPos[1] < 0 ){ mult[1]--; }
|
||||
|
||||
y = (mult[1] * imgSize[1]) - paralaxPos[1];
|
||||
while(y < gameSize[1]){
|
||||
x = (mult[0] * imgSize[0]) - paralaxPos[0];
|
||||
while(x < gameSize[0]){
|
||||
Draw_DrawImgResized(img, x, y, imgSize[0], imgSize[1]);
|
||||
x += imgSize[0];
|
||||
}
|
||||
y += imgSize[1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_SetColor
|
||||
//
|
||||
|
||||
@@ -125,6 +125,13 @@ void Draw_DrawImgPart(DrawImg img,int x,int y,int w,int h,int i,int j);
|
||||
void Draw_DrawImgPartHoriz(DrawImg img,int x,int y,int w,int i);
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_ImgParallax
|
||||
//
|
||||
//
|
||||
void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2], int gamePos[2], int gameSize[2]);
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Draw_SetColor
|
||||
//
|
||||
|
||||
@@ -40,6 +40,17 @@ long long t_draw;
|
||||
int fproc_count;
|
||||
int fdraw_count;
|
||||
|
||||
typedef struct TParallaxBackground TParallaxBackground, *ParallaxBackground;
|
||||
struct TParallaxBackground {
|
||||
DrawImg img;
|
||||
int imgSize[2];
|
||||
int imgOffset[2];
|
||||
float parallaxFactor[2];
|
||||
};
|
||||
#define MaxParallaxBackgrounds 10
|
||||
TParallaxBackground _parallaxBackground[MaxParallaxBackgrounds];
|
||||
int _nParallaxBackgrounds=0;
|
||||
|
||||
int gamelib_debug=0;
|
||||
|
||||
|
||||
@@ -351,17 +362,29 @@ void GameLib_DrawLoop(void *data, float f){
|
||||
int i;
|
||||
int game_pos[2];
|
||||
|
||||
game_pos[0]=_game_pos0[0]+f*(_game_pos1[0]-_game_pos0[0]);
|
||||
game_pos[1]=_game_pos0[1]+f*(_game_pos1[1]-_game_pos0[1]);
|
||||
GameLib_GetPosInstant(game_pos,f);
|
||||
|
||||
time=Time_GetTime();
|
||||
|
||||
// Predibujado
|
||||
// PreDraw
|
||||
if(_gamepredraw){
|
||||
_gamepredraw(f);
|
||||
}else{
|
||||
// Limpiar pantalla
|
||||
Draw_Clean(0,0,0);
|
||||
if(_nParallaxBackgrounds==0){
|
||||
// Clean screen
|
||||
Draw_Clean(0,0,0);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw parallax backgrounds
|
||||
for(i=0;i<_nParallaxBackgrounds;i++){
|
||||
Draw_ImgParallax(
|
||||
_parallaxBackground[i].img,
|
||||
_parallaxBackground[i].imgSize,
|
||||
_parallaxBackground[i].imgOffset,
|
||||
_parallaxBackground[i].parallaxFactor,
|
||||
game_pos,
|
||||
_game_size);
|
||||
}
|
||||
|
||||
// Draw entities
|
||||
@@ -446,6 +469,7 @@ void GameLib_Loop(
|
||||
// GameLib_SetPos
|
||||
// GameLib_UpdatePos
|
||||
// GameLib_SetPos
|
||||
// GameLib_GetPosInstant
|
||||
//
|
||||
//
|
||||
void GameLib_GetPos(int pos[2]){
|
||||
@@ -466,6 +490,10 @@ void GameLib_GetSize(int size[2]){
|
||||
size[0]=_game_size[0];
|
||||
size[1]=_game_size[1];
|
||||
}
|
||||
void GameLib_GetPosInstant(int pos[2],float f){
|
||||
pos[0]=_game_pos0[0]+f*(_game_pos1[0]-_game_pos0[0]);
|
||||
pos[1]=_game_pos0[1]+f*(_game_pos1[1]-_game_pos0[1]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -659,3 +687,33 @@ void GameLib_ConvertScreenPositionToGamePosition(
|
||||
gamePos[1]=(screenPos[1]*_game_size[1])+game_pos[1];
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_AddParallaxBackground
|
||||
//
|
||||
//
|
||||
void GameLib_AddParallaxBackground(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2]){
|
||||
int idx = _nParallaxBackgrounds;
|
||||
if((idx+1)>=MaxParallaxBackgrounds){
|
||||
Print("GameLib: Can't add parallaxBackground, limit reached.");
|
||||
return;
|
||||
}
|
||||
_parallaxBackground[idx].img=img;
|
||||
_parallaxBackground[idx].imgSize[0]=imgSize[0];
|
||||
_parallaxBackground[idx].imgSize[1]=imgSize[1];
|
||||
_parallaxBackground[idx].imgOffset[0]=imgOffset[0];
|
||||
_parallaxBackground[idx].imgOffset[1]=imgOffset[1];
|
||||
_parallaxBackground[idx].parallaxFactor[0]=parallaxFactor[0];
|
||||
_parallaxBackground[idx].parallaxFactor[1]=parallaxFactor[1];
|
||||
_nParallaxBackgrounds++;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_CleanParallaxBackgrounds
|
||||
//
|
||||
//
|
||||
void GameLib_CleanParallaxBackgrounds(){
|
||||
_nParallaxBackgrounds=0;
|
||||
}
|
||||
|
||||
|
||||
@@ -56,12 +56,14 @@ void GameLib_Loop(
|
||||
// GameLib_SetPos
|
||||
// GameLib_UpdatePos
|
||||
// GameLib_SetPos
|
||||
// GameLib_GetPosInstant
|
||||
//
|
||||
//
|
||||
void GameLib_GetPos(int pos[2]);
|
||||
void GameLib_SetPos(int pos[2]);
|
||||
void GameLib_UpdatePos(int pos[2]);
|
||||
void GameLib_GetSize(int size[2]);
|
||||
void GameLib_GetPosInstant(int pos[2],float f);
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
@@ -132,4 +134,17 @@ void GameLib_ConvertScreenPositionToGamePosition(
|
||||
vec2 screenPos, vec2 gamePos);
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_AddParallaxBackground
|
||||
//
|
||||
//
|
||||
void GameLib_AddParallaxBackground(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2]);
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// GameLib_CleanParallaxBackgrounds
|
||||
//
|
||||
//
|
||||
void GameLib_CleanParallaxBackgrounds();
|
||||
|
||||
#endif
|
||||
|
||||
BIN
data/background.png
Normal file
BIN
data/background.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
Reference in New Issue
Block a user