From 195fb39210b83f8f6af024843edea26e0c4392b8 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 1 Mar 2023 18:57:45 +0100 Subject: [PATCH] =?UTF-8?q?-=20Treballant=20en=20la=20ordenaci=C3=B3=20de?= =?UTF-8?q?=20actors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 27 ++++++++++++++ data/test.gif | Bin 1304 -> 1365 bytes source/actor.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++ source/actor.h | 38 ++++++++++++++++++++ source/jdraw.cpp | 2 +- source/jfile.cpp | 2 +- source/jgame.cpp | 19 +++++++++- source/jgame.h | 3 +- source/main.cpp | 20 +++++++++-- 9 files changed, 194 insertions(+), 7 deletions(-) create mode 100644 Makefile create mode 100644 source/actor.cpp create mode 100644 source/actor.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8777782 --- /dev/null +++ b/Makefile @@ -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" diff --git a/data/test.gif b/data/test.gif index 4c4d567c4c9acd615436c1a8c9ef898ea3abb912..fbbff09af15084b78e059af3a6633946bd758649 100644 GIT binary patch delta 1245 zcmV<31S0#G3e^gbh<^&jb6Uzh9geGbrp<1p$S*iNZmSehGqj6Xf8F?#R*I0@>R?ZI zd4wKtHCtI)f@=;`gN2b9dx%MjjSh&6F$Pba2A`py2%V$`cnPTq8gg5e3YmhMHEb)S zrnjKDxuI>T55Nzp7+Fa-h_JGPCABKJp}V@Jw_?A+*TWUX$A7P}%Tx!=DWl||=I7H^ z*4OO9s}qNnkI84U;heXo)1K1$OzZ6eW}}p(OENmk#Gw!+?c6S0MF0kz=uT0kZs49| zS;J{sDRiU%WSv?fks`2c5)|>NK~ED$8T&js-1kIX0+T1-S+E$#TR1R0l#%m9R9Ur~ z0(DMON$+J)k$<_0<`d)#X-*J|U^(p}R3EyjHQlIEid8Fo1!aQF5v5th ztz9Q@c|n_cw=V#=x&Vu0>$mOTy$|yhwi_2DNVtpLHfH$MXkvZuq-ya4~sBi?)3HN^+UW zn*E4*!6y9d-@|`B~Qqf19W5B< z4w-nM2!9%0$YF#ZzUa`1C$I4(KG3I?AHsm%%7==8!3-sUnJ<6ggpyM1ncziXqqt zCWw08h-H==vMCpoWtK^2n_C)+CkQ#NDJYqDK7UGOpptrc~oPWt)^%m}#nc4 zI)9<80M?tXoTNZ#=f28@_^+q7y4x(l0kiloz|vN@Fh$(@n{2xhl${PT@LFBxvb zs_N{q%O-n#WvhV^?IyJX?>XVW1`A8{w0}%&fdHrIN`J7~ImMqTKzX~I3YrnHSY^0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R zWx>SS+pel=dOIMD&VJw~wZN|Iu#$0Z%V@~{QvC5wB3CG@#TNs~X}>)ak}1%7&TJUO ztGyV9i&pm78xlS%PrIRn7 zxW##flKCO6;I;>Ptu=oe`scLPXriKydk#6(alXoC?0>!~Ik&2#N%@#6S7|EDQGX`Q zx2NhNj{3*9?~bGEo0ima@I)Rwoyb*Z&M&1sa|O4pwhq5C_t=|Fbm_v6A9ha0ZP1|e z(%ad1%IQ}CU4%z7cQ)_w=db_%{P*ww{{Rf200&6G0vhmu2uz>?7s$W{I`DxIjGzQ3 yNWltP@PZi3pawU{!47)xgCGo{2pLC6!V;SBgeXj*3RlR&7P|0-FqB{f00299!&~J5 diff --git a/source/actor.cpp b/source/actor.cpp new file mode 100644 index 0000000..880fb6e --- /dev/null +++ b/source/actor.cpp @@ -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); + } + +} \ No newline at end of file diff --git a/source/actor.h b/source/actor.h new file mode 100644 index 0000000..d5f0ac3 --- /dev/null +++ b/source/actor.h @@ -0,0 +1,38 @@ +#pragma once +#include + +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); + +} \ No newline at end of file diff --git a/source/jdraw.cpp b/source/jdraw.cpp index b648502..2df06df 100644 --- a/source/jdraw.cpp +++ b/source/jdraw.cpp @@ -252,7 +252,7 @@ namespace draw { 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 - 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) SDL_LockTexture(sdl_texture, NULL, (void **)&sdl_pixels, &sdl_pitch); diff --git a/source/jfile.cpp b/source/jfile.cpp index 87cc813..e1c5123 100644 --- a/source/jfile.cpp +++ b/source/jfile.cpp @@ -163,7 +163,7 @@ namespace file fi.seekg(toc_offset); // 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 uint32_t file_offset, file_size; diff --git a/source/jgame.cpp b/source/jgame.cpp index 3af5529..b998843 100644 --- a/source/jgame.cpp +++ b/source/jgame.cpp @@ -3,11 +3,23 @@ #include "jinput.h" #include +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[]) { game::init(); input::init(); + static unsigned int current_ticks = SDL_GetTicks(); + bool should_exit=false; SDL_Event e; while (!should_exit) @@ -26,7 +38,12 @@ int main(int argc, char *argv[]) } } 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; } \ No newline at end of file diff --git a/source/jgame.h b/source/jgame.h index 5162d3a..3b6ee18 100644 --- a/source/jgame.h +++ b/source/jgame.h @@ -2,7 +2,8 @@ namespace game { - + void setUpdateTicks(const int ticks); + void init(); bool loop(); diff --git a/source/main.cpp b/source/main.cpp index 9c73af8..22b76c8 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -2,8 +2,10 @@ #include "jdraw.h" #include "jinput.h" #include +#include "actor.h" draw::surface *surf; +actor::actor_t *box; void game::init() { @@ -11,14 +13,25 @@ void game::init() surf = draw::loadSurface("test.gif"); draw::setSource(surf); 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; 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); 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+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(); return true;