- Treballant en la ordenació de actors

This commit is contained in:
2023-03-01 18:57:45 +01:00
parent f54e5ebef5
commit 195fb39210
9 changed files with 194 additions and 7 deletions

27
Makefile Normal file
View File

@@ -0,0 +1,27 @@
executable = thepool
source = source/*.cpp
windows:
@echo off
g++ $(source) icon.res -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lmingw32 -lSDL2main -lSDL2 -lSDL2_mixer -mwindows -o "$(executable).exe"
strip -s -R .comment -R .gnu.version --strip-unneeded "$(executable).exe"
windows_debug:
@echo off
g++ $(source) -D DEBUG -g -Wall -fvar-tracking -lmingw32 -lSDL2main -lSDL2 -lSDL2_mixer -o "$(executable)_debug.exe"
macos:
clang++ $(source) -Wall -Os -std=c++11 -ffunction-sections -fdata-sections -lSDL2 -lSDL2_mixer -o "$(executable)"
macos_debug:
clang++ $(source) -D DEBUG -g -Wall -Os -std=c++11 -ffunction-sections -fdata-sections -lSDL2 -lSDL2_mixer -o "$(executable)_debug"
macos_bundle:
clang++ $(source) -D MACOS_BUNDLE -Wall -Os -std=c++11 -framework SDL2 -framework SDL2_mixer -F /Library/Frameworks -ffunction-sections -fdata-sections -o mini_bundle -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos10.12
linux:
g++ $(source) -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -lSDL2_mixer -o "$(executable)"
strip -s -R .comment -R .gnu.version --strip-unneeded "$(executable)"
linux_debug:
g++ $(source) -D DEBUG -g -Wall -Os -ffunction-sections -fdata-sections -Wl,--gc-sections -lSDL2 -lSDL2_mixer -o "$(executable)_debug"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

90
source/actor.cpp Normal file
View File

@@ -0,0 +1,90 @@
#include "actor.h"
#include "jdraw.h"
namespace actor
{
actor_t *first = nullptr;
actor_t *dirty = nullptr;
actor_t *getFirst()
{
return first;
}
actor_t *create(pos_t p, size_t s, SDL_Rect r, SDL_Point o)
{
actor_t *act = (actor_t*)malloc(sizeof(actor_t));
act->pos = p;
act->size = s;
act->bmp_rect = r;
act->bmp_offset = o;
act->prev = act->next = nullptr;
return act;
}
void setDirty(actor_t *act)
{
if (act->prev) act->prev->next = act->next;
if (act->next) act->next->prev = act->prev;
if (act == first) first = act->next;
act->next = dirty;
dirty = act;
}
void reorder()
{
while (dirty)
{
const int z_index = dirty->pos.x + dirty->pos.y + dirty->pos.z;
if (first)
{
actor_t *current = first;
while (true)
{
const int z_index2 = current->pos.x + current->pos.y + current->pos.z;
if (z_index > z_index2)
{
if (current->next)
{
current = current->next;
}
else
{
current->next = dirty;
dirty = dirty->next;
current->next->prev = current;
current->next->next = nullptr;
break;
}
}
else
{
dirty->prev = current->prev;
current->prev = dirty;
if (dirty->prev) dirty->prev->next = dirty;
dirty = dirty->next;
current->prev->next = current;
break;
}
}
}
else
{
first = dirty;
dirty = dirty->next;
first->prev=first->next=nullptr;
}
}
}
void draw(actor_t *act, const bool draw_all)
{
if (!act) return;
const int x = 148-act->bmp_offset.x + act->pos.x*2 - act->pos.y*2;
const int y = 91-act->bmp_offset.y + act->pos.x + act->pos.y;
draw::draw(x, y, act->bmp_rect.w, act->bmp_rect.h, act->bmp_rect.x, act->bmp_rect.y);
if (draw_all && act->next) draw(act->next);
}
}

38
source/actor.h Normal file
View File

@@ -0,0 +1,38 @@
#pragma once
#include <SDL2/SDL.h>
namespace actor
{
struct pos_t
{
int x, y, z;
};
struct size_t
{
int w, h, d;
};
struct actor_t
{
SDL_Rect bmp_rect;
SDL_Point bmp_offset;
pos_t pos;
size_t size;
actor_t *prev;
actor_t *next;
};
actor_t *getFirst();
actor_t *create(pos_t p, size_t s, SDL_Rect r, SDL_Point o);
void setDirty(actor_t *act);
void reorder();
void draw(actor_t *act, const bool draw_all=true);
}

View File

@@ -252,7 +252,7 @@ namespace draw
{ {
Uint32 *sdl_pixels; // Punter al array de pixels que enstornarà SDL_LockTexture Uint32 *sdl_pixels; // Punter al array de pixels que enstornarà SDL_LockTexture
int sdl_pitch; // Ací estarà guardat el pitch de la textura, com es de 32 bits, no m'afecta int sdl_pitch; // Ací estarà guardat el pitch de la textura, com es de 32 bits, no m'afecta
const int size = screen->w * screen->h; // tamany de la superficie const uint32_t size = screen->w * screen->h; // tamany de la superficie
// Bloquejem la textura SDL i agafem els seus pixels (son enters de 32 bits amb format 0xAARRGGBB) // Bloquejem la textura SDL i agafem els seus pixels (son enters de 32 bits amb format 0xAARRGGBB)
SDL_LockTexture(sdl_texture, NULL, (void **)&sdl_pixels, &sdl_pitch); SDL_LockTexture(sdl_texture, NULL, (void **)&sdl_pixels, &sdl_pitch);

View File

@@ -163,7 +163,7 @@ namespace file
fi.seekg(toc_offset); fi.seekg(toc_offset);
// Per a cada arxiu inclos en l'arxiu de recursos... // Per a cada arxiu inclos en l'arxiu de recursos...
for (int i = 0; i < num_files; ++i) for (unsigned int i = 0; i < num_files; ++i)
{ {
// Llegim en quina posició està i quant copua // Llegim en quina posició està i quant copua
uint32_t file_offset, file_size; uint32_t file_offset, file_size;

View File

@@ -3,11 +3,23 @@
#include "jinput.h" #include "jinput.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
namespace game
{
static unsigned int ticks_per_frame = 1000/60;
void setUpdateTicks(const int ticks)
{
ticks_per_frame = ticks;
}
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
game::init(); game::init();
input::init(); input::init();
static unsigned int current_ticks = SDL_GetTicks();
bool should_exit=false; bool should_exit=false;
SDL_Event e; SDL_Event e;
while (!should_exit) while (!should_exit)
@@ -26,7 +38,12 @@ int main(int argc, char *argv[])
} }
} }
input::update(keydown,keyp); input::update(keydown,keyp);
if (!game::loop()) should_exit = true;
if (SDL_GetTicks()-current_ticks >= game::ticks_per_frame)
{
if (!game::loop()) should_exit = true;
current_ticks = SDL_GetTicks();
}
} }
return 0; return 0;
} }

View File

@@ -2,7 +2,8 @@
namespace game namespace game
{ {
void setUpdateTicks(const int ticks);
void init(); void init();
bool loop(); bool loop();

View File

@@ -2,8 +2,10 @@
#include "jdraw.h" #include "jdraw.h"
#include "jinput.h" #include "jinput.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "actor.h"
draw::surface *surf; draw::surface *surf;
actor::actor_t *box;
void game::init() void game::init()
{ {
@@ -11,14 +13,25 @@ void game::init()
surf = draw::loadSurface("test.gif"); surf = draw::loadSurface("test.gif");
draw::setSource(surf); draw::setSource(surf);
draw::loadPalette("test.gif"); draw::loadPalette("test.gif");
game::setUpdateTicks(64);
box = actor::create({0,0,0}, {6,6,6}, {24,0,24,24}, {0,24});
actor::setDirty(box);
box = actor::create({6,0,0}, {6,6,6}, {24,0,24,24}, {0,24});
actor::setDirty(box);
actor::reorder();
} }
int sx=1, sy=0; int sx=1, sy=0;
bool game::loop() bool game::loop()
{ {
if (input::keyDown(SDL_SCANCODE_LEFT)) sx++; if (input::keyDown(SDL_SCANCODE_LEFT) && box->pos.x>0) { box->pos.x--; actor::setDirty(box); }
if (input::keyDown(SDL_SCANCODE_RIGHT) && box->pos.x<42) { box->pos.x++; actor::setDirty(box); }
if (input::keyDown(SDL_SCANCODE_UP) && box->pos.y>0) { box->pos.y--; actor::setDirty(box); }
if (input::keyDown(SDL_SCANCODE_DOWN) && box->pos.y<42) { box->pos.y++; actor::setDirty(box); }
actor::reorder();
draw::cls(8); draw::cls(8);
for (int y=0;y<8;++y) for (int y=0;y<8;++y)
{ {
@@ -27,7 +40,8 @@ bool game::loop()
draw::draw(148+x*12-y*12,80+x*6+y*6,24,11,0,13); draw::draw(148+x*12-y*12,80+x*6+y*6,24,11,0,13);
} }
} }
draw::draw(148+sx*2-sy*2, 68+sx+sy,24,24,24,0); actor::draw(actor::getFirst());
//draw::draw(148+sx*2-sy*2, 67+sx+sy,24,24,24,0);
draw::render(); draw::render();
return true; return true;