Separated drawing routines to own files. Monster and bullet tiles are masked. Monster position is origin at center. Monsters dont fire if within solid tile.
This commit is contained in:
parent
01617a251b
commit
cf8d944a80
|
@ -0,0 +1,101 @@
|
||||||
|
#include "draw.h"
|
||||||
|
|
||||||
|
extern game_state_t* gs;
|
||||||
|
extern game_assets_t* g_assets;
|
||||||
|
|
||||||
|
// draw level at current view
|
||||||
|
void Draw_World( SDL_Renderer* r ) {
|
||||||
|
SDL_Rect dst = {0,0,320,200};
|
||||||
|
// solid BG fill
|
||||||
|
SDL_RenderCopy( r, g_assets->tile_tx[0], NULL, &dst );
|
||||||
|
// draw level view 20x10 tiles at 16x16px offset one tile in y
|
||||||
|
for ( int j = 0; j < 10; ++j ) {
|
||||||
|
dst.y = TILE_SIZE + j * TILE_SIZE;
|
||||||
|
dst.w = TILE_SIZE; dst.h = TILE_SIZE;
|
||||||
|
for ( int i = 0; i < 20; ++i ) {
|
||||||
|
dst.x = i * TILE_SIZE;
|
||||||
|
uint8_t til = gs->levels[gs->current_level].tiles[j * 100 + gs->view_x + i];
|
||||||
|
if ( til == 0 ) continue;
|
||||||
|
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// draw player
|
||||||
|
void Draw_Player( SDL_Renderer* r ) {
|
||||||
|
SDL_Rect dst;
|
||||||
|
// relative to view
|
||||||
|
dst.x = gs->ps.px - gs->view_x * TILE_SIZE;
|
||||||
|
dst.y = TILE_SIZE + gs->ps.py;
|
||||||
|
// tile 56 neutral, 53 right, 57 left 20x16px
|
||||||
|
uint8_t til = 56;
|
||||||
|
dst.w = 20; dst.h = 16;
|
||||||
|
|
||||||
|
// jetpack tile
|
||||||
|
if ( gs->ps.do_jetpack )
|
||||||
|
til = gs->ps.last_dir >= 0 ? 77 : 80;
|
||||||
|
// grounded walk tile
|
||||||
|
else if ( gs->ps.on_ground ) {
|
||||||
|
til = gs->ps.last_dir >= 0 ? 53 : 57;
|
||||||
|
}
|
||||||
|
// jump tile
|
||||||
|
else if ( gs->ps.do_jump || !gs->ps.on_ground )
|
||||||
|
til = gs->ps.last_dir >= 0 ? 67 : 68;
|
||||||
|
|
||||||
|
// render
|
||||||
|
// grounded debug
|
||||||
|
if ( gs->ps.on_ground ) {
|
||||||
|
SDL_SetRenderDrawColor( r, 255, 0, 255, 255 );
|
||||||
|
SDL_RenderDrawLine( r, dst.x, dst.y+dst.h, dst.x+dst.w, dst.y+dst.h );
|
||||||
|
}
|
||||||
|
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
||||||
|
}
|
||||||
|
|
||||||
|
// draw player bullet
|
||||||
|
void Draw_Bullet( SDL_Renderer* r ) {
|
||||||
|
SDL_Rect dst;
|
||||||
|
if ( gs->ps.bullet_px && gs->ps.bullet_py ) {
|
||||||
|
// relative to view
|
||||||
|
dst.x = gs->ps.bullet_px - gs->view_x * TILE_SIZE;
|
||||||
|
dst.y = TILE_SIZE + gs->ps.bullet_py;
|
||||||
|
// tile 127 right, 128 left
|
||||||
|
uint8_t til = gs->ps.bullet_dir > 0 ? 127 : 128;
|
||||||
|
dst.w = 12; dst.h = 3;
|
||||||
|
|
||||||
|
// render
|
||||||
|
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// draw monsters
|
||||||
|
void Draw_Monsters( SDL_Renderer* r ) {
|
||||||
|
SDL_Rect dst;
|
||||||
|
uint8_t til = 0;
|
||||||
|
|
||||||
|
for ( int i = 0; i < sizeof(gs->ms) / sizeof(gs->ms[0]); ++i ) {
|
||||||
|
monster_state_t* m = &gs->ms[i];
|
||||||
|
if ( m->type ) {
|
||||||
|
dst.x = m->px - gs->view_x * TILE_SIZE - 12;
|
||||||
|
dst.y = TILE_SIZE + m->py - 10;
|
||||||
|
dst.w = 24; dst.h = 20;
|
||||||
|
til = m->type;
|
||||||
|
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// monster bullets
|
||||||
|
void Draw_MonsterBullet( SDL_Renderer* r ) {
|
||||||
|
SDL_Rect dst;
|
||||||
|
if ( gs->mbullet_px && gs->mbullet_py ) {
|
||||||
|
// relative to view
|
||||||
|
dst.x = gs->mbullet_px - gs->view_x * TILE_SIZE;
|
||||||
|
dst.y = TILE_SIZE + gs->mbullet_py;
|
||||||
|
// tile 127 right, 128 left
|
||||||
|
uint8_t til = gs->mbullet_dir > 0 ? 121 : 124;
|
||||||
|
dst.w = 20; dst.h = 3;
|
||||||
|
|
||||||
|
// render
|
||||||
|
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
// drawing
|
||||||
|
#ifndef __DRAW_H
|
||||||
|
#define __DRAW_H
|
||||||
|
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
|
void Draw_World( SDL_Renderer* r );
|
||||||
|
void Draw_Player( SDL_Renderer* r );
|
||||||
|
void Draw_Bullet( SDL_Renderer* r );
|
||||||
|
void Draw_Monsters( SDL_Renderer* r );
|
||||||
|
void Draw_MonsterBullet( SDL_Renderer* r );
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
102
game.c
102
game.c
|
@ -1,5 +1,6 @@
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
|
#include "draw.h"
|
||||||
|
|
||||||
// global game state
|
// global game state
|
||||||
game_state_t* gs = NULL;
|
game_state_t* gs = NULL;
|
||||||
|
@ -35,10 +36,11 @@ static void G_InitAssets( SDL_Renderer* r ) {
|
||||||
|
|
||||||
// convert each surface from util's array to g_asset texture array
|
// convert each surface from util's array to g_asset texture array
|
||||||
for ( int i = 0; i < NUM_EXE_TILES; ++i ) {
|
for ( int i = 0; i < NUM_EXE_TILES; ++i ) {
|
||||||
// mask dave sprites
|
// mask player, monster, and bullet sprites
|
||||||
if ( (i >= 53 && i <= 59) || (i >= 67 && i <= 68)
|
if ( (i >= 53 && i <= 59) || (i >= 67 && i <= 68)
|
||||||
|| (i >= 71 && i <= 73) || (i >= 77 && i <= 82) )
|
|| (i >= 71 && i <= 73) || (i >= 77 && i <= 82) || (i >= 89 && i <= 132) )
|
||||||
SDL_SetColorKey( tileSfcs[i], 1, SDL_MapRGB( tileSfcs[i]->format, 0, 0, 0 ) );
|
SDL_SetColorKey( tileSfcs[i], 1, SDL_MapRGB( tileSfcs[i]->format, 0, 0, 0 ) );
|
||||||
|
|
||||||
g_assets->tile_tx[i] = SDL_CreateTextureFromSurface( r, tileSfcs[i] );
|
g_assets->tile_tx[i] = SDL_CreateTextureFromSurface( r, tileSfcs[i] );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,102 +134,6 @@ static void G_Update() {
|
||||||
G_ClearInput();
|
G_ClearInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw level at current view
|
|
||||||
void Draw_World( SDL_Renderer* r ) {
|
|
||||||
SDL_Rect dst = {0,0,320,200};
|
|
||||||
// solid BG fill
|
|
||||||
SDL_RenderCopy( r, g_assets->tile_tx[0], NULL, &dst );
|
|
||||||
// draw level view 20x10 tiles at 16x16px offset one tile in y
|
|
||||||
for ( int j = 0; j < 10; ++j ) {
|
|
||||||
dst.y = TILE_SIZE + j * TILE_SIZE;
|
|
||||||
dst.w = TILE_SIZE; dst.h = TILE_SIZE;
|
|
||||||
for ( int i = 0; i < 20; ++i ) {
|
|
||||||
dst.x = i * TILE_SIZE;
|
|
||||||
uint8_t til = gs->levels[gs->current_level].tiles[j * 100 + gs->view_x + i];
|
|
||||||
if ( til == 0 ) continue;
|
|
||||||
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// draw monsters
|
|
||||||
void Draw_Monsters( SDL_Renderer* r ) {
|
|
||||||
SDL_Rect dst;
|
|
||||||
uint8_t til = 0;
|
|
||||||
|
|
||||||
for ( int i = 0; i < sizeof(gs->ms) / sizeof(gs->ms[0]); ++i ) {
|
|
||||||
monster_state_t* m = &gs->ms[i];
|
|
||||||
if ( m->type ) {
|
|
||||||
dst.x = m->px - gs->view_x * TILE_SIZE;
|
|
||||||
dst.y = TILE_SIZE + m->py;
|
|
||||||
dst.w = 20; dst.h = 16;
|
|
||||||
til = m->type;
|
|
||||||
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// monster bullets
|
|
||||||
void Draw_MonsterBullet( SDL_Renderer* r ) {
|
|
||||||
SDL_Rect dst;
|
|
||||||
if ( gs->mbullet_px && gs->mbullet_py ) {
|
|
||||||
// relative to view
|
|
||||||
dst.x = gs->mbullet_px - gs->view_x * TILE_SIZE;
|
|
||||||
dst.y = TILE_SIZE + gs->mbullet_py;
|
|
||||||
// tile 127 right, 128 left
|
|
||||||
uint8_t til = gs->mbullet_dir > 0 ? 121 : 124;
|
|
||||||
dst.w = 12; dst.h = 3;
|
|
||||||
|
|
||||||
// render
|
|
||||||
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// draw player
|
|
||||||
void Draw_Player( SDL_Renderer* r ) {
|
|
||||||
SDL_Rect dst;
|
|
||||||
// relative to view
|
|
||||||
dst.x = gs->ps.px - gs->view_x * TILE_SIZE;
|
|
||||||
dst.y = TILE_SIZE + gs->ps.py;
|
|
||||||
// tile 56 neutral, 53 right, 57 left 20x16px
|
|
||||||
uint8_t til = 56;
|
|
||||||
dst.w = 20; dst.h = 16;
|
|
||||||
|
|
||||||
// jetpack tile
|
|
||||||
if ( gs->ps.do_jetpack )
|
|
||||||
til = gs->ps.last_dir >= 0 ? 77 : 80;
|
|
||||||
// grounded walk tile
|
|
||||||
else if ( gs->ps.on_ground ) {
|
|
||||||
til = gs->ps.last_dir >= 0 ? 53 : 57;
|
|
||||||
}
|
|
||||||
// jump tile
|
|
||||||
else if ( gs->ps.do_jump || !gs->ps.on_ground )
|
|
||||||
til = gs->ps.last_dir >= 0 ? 67 : 68;
|
|
||||||
|
|
||||||
// render
|
|
||||||
// grounded debug
|
|
||||||
if ( gs->ps.on_ground ) {
|
|
||||||
SDL_SetRenderDrawColor( r, 255, 0, 255, 255 );
|
|
||||||
SDL_RenderDrawLine( r, dst.x, dst.y+dst.h, dst.x+dst.w, dst.y+dst.h );
|
|
||||||
}
|
|
||||||
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
|
||||||
}
|
|
||||||
|
|
||||||
// draw player bullet
|
|
||||||
void Draw_Bullet( SDL_Renderer* r ) {
|
|
||||||
SDL_Rect dst;
|
|
||||||
if ( gs->ps.bullet_px && gs->ps.bullet_py ) {
|
|
||||||
// relative to view
|
|
||||||
dst.x = gs->ps.bullet_px - gs->view_x * TILE_SIZE;
|
|
||||||
dst.y = TILE_SIZE + gs->ps.bullet_py;
|
|
||||||
// tile 127 right, 128 left
|
|
||||||
uint8_t til = gs->ps.bullet_dir > 0 ? 127 : 128;
|
|
||||||
dst.w = 12; dst.h = 3;
|
|
||||||
|
|
||||||
// render
|
|
||||||
SDL_RenderCopy( r, g_assets->tile_tx[til], NULL, &dst );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// main drawing routine
|
// main drawing routine
|
||||||
static void G_Draw( SDL_Renderer* r ) {
|
static void G_Draw( SDL_Renderer* r ) {
|
||||||
// clear backbuffer
|
// clear backbuffer
|
||||||
|
|
16
monster.c
16
monster.c
|
@ -19,10 +19,10 @@ void M_Move() {
|
||||||
m->path_index = 2;
|
m->path_index = 2;
|
||||||
}
|
}
|
||||||
// move
|
// move
|
||||||
if ( m->npx < 0 ) { m->px -= 1; m->npx++; }
|
if ( m->npx < 0 ) { m->px -= 1; m->npx += 1; }
|
||||||
if ( m->npx > 0 ) { m->px += 1; m->npx--; }
|
if ( m->npx > 0 ) { m->px += 1; m->npx -= 1; }
|
||||||
if ( m->npy < 0 ) { m->py -= 1; m->npy++; }
|
if ( m->npy < 0 ) { m->py -= 1; m->npy += 1; }
|
||||||
if ( m->npy > 0 ) { m->py += 1; m->npy--; }
|
if ( m->npy > 0 ) { m->py += 1; m->npy -= 1; }
|
||||||
// update tile pos
|
// update tile pos
|
||||||
m->tx = m->px / TILE_SIZE;
|
m->tx = m->px / TILE_SIZE;
|
||||||
m->ty = m->py / TILE_SIZE;
|
m->ty = m->py / TILE_SIZE;
|
||||||
|
@ -34,13 +34,13 @@ void M_Fire() {
|
||||||
if ( !gs->mbullet_px && !gs->mbullet_py ) {
|
if ( !gs->mbullet_px && !gs->mbullet_py ) {
|
||||||
for ( int i = 0; i < sizeof(gs->ms) / sizeof(gs->ms[0]); ++i ) {
|
for ( int i = 0; i < sizeof(gs->ms) / sizeof(gs->ms[0]); ++i ) {
|
||||||
monster_state_t* m = &gs->ms[i];
|
monster_state_t* m = &gs->ms[i];
|
||||||
if ( m->type && W_IsVisible( m->px ) ) {
|
if ( m->type && W_IsVisible( m->px ) && W_IsClear( m->px, m->py, 0 ) ) {
|
||||||
gs->mbullet_dir = gs->ps.px < m->px ? -1 : 1;
|
gs->mbullet_dir = gs->ps.px < m->px ? -1 : 1;
|
||||||
if ( gs->mbullet_dir == 1 )
|
if ( gs->mbullet_dir == 1 )
|
||||||
gs->mbullet_px = m->px + 18;
|
gs->mbullet_px = m->px;
|
||||||
if ( gs->mbullet_dir == -1 )
|
if ( gs->mbullet_dir == -1 )
|
||||||
gs->mbullet_px = m->px - 8;
|
gs->mbullet_px = m->px - 20;
|
||||||
gs->mbullet_py = m->py + 8;
|
gs->mbullet_py = m->py;// + 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue