Skip to content

Commit

Permalink
Add region system
Browse files Browse the repository at this point in the history
three new commands: setregion(id,x,y,x2,y2), changeregion(id), removeregion(id)
  • Loading branch information
mothbeanie committed Aug 24, 2023
1 parent 4148234 commit bf8ca30
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 112 deletions.
42 changes: 7 additions & 35 deletions desktop_version/src/CustomLevels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1656,35 +1656,7 @@ bool customlevelclass::save(const std::string& _path)

void customlevelclass::generatecustomminimap(void)
{
map.customzoom = 1;
if (mapwidth <= 10 && mapheight <= 10)
{
map.customzoom = 2;
}
if (mapwidth <= 5 && mapheight <= 5)
{
map.customzoom = 4;
}

// Set minimap offsets
switch (map.customzoom)
{
case 4:
map.custommmxoff = 24 * (5 - mapwidth);
map.custommmyoff = 18 * (5 - mapheight);
break;
case 2:
map.custommmxoff = 12 * (10 - mapwidth);
map.custommmyoff = 9 * (10 - mapheight);
break;
default:
map.custommmxoff = 6 * (20 - mapwidth);
map.custommmyoff = int(4.5 * (20 - mapheight));
break;
}

map.custommmxsize = 240 - (map.custommmxoff * 2);
map.custommmysize = 180 - (map.custommmyoff * 2);
const MapRenderData data = map.get_render_data();

// Start drawing the minimap

Expand All @@ -1693,22 +1665,22 @@ void customlevelclass::generatecustomminimap(void)
graphics.clear();

// Scan over the map size
for (int j2 = 0; j2 < mapheight; j2++)
for (int j2 = data.starty; j2 < data.starty + data.height; j2++)
{
for (int i2 = 0; i2 < mapwidth; i2++)
for (int i2 = data.startx; i2 < data.startx + data.width; i2++)
{
std::vector<SDL_Point> dark_points;
std::vector<SDL_Point> light_points;

bool dark = getroomprop(i2, j2)->tileset == 1;

// Ok, now scan over each square
for (int j = 0; j < 9 * map.customzoom; j++)
for (int j = 0; j < 9 * data.zoom; j++)
{
for (int i = 0; i < 12 * map.customzoom; i++)
for (int i = 0; i < 12 * data.zoom; i++)
{
int tile;
switch (map.customzoom)
switch (data.zoom)
{
case 4:
tile = absfree(
Expand All @@ -1733,7 +1705,7 @@ void customlevelclass::generatecustomminimap(void)
if (tile >= 1)
{
// Add this pixel
SDL_Point point = { (i2 * 12 * map.customzoom) + i, (j2 * 9 * map.customzoom) + j };
SDL_Point point = { ((i2 - data.startx) * 12 * data.zoom) + i, ((j2 - data.starty) * 9 * data.zoom) + j };
if (dark)
{
dark_points.push_back(point);
Expand Down
4 changes: 2 additions & 2 deletions desktop_version/src/FileSystemUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ bool FILESYSTEM_isMounted(const char* filename)
return PHYSFS_getMountPoint(filename) != NULL;
}

static bool FILESYSTEM_exists(const char *fname)
static bool FILESYSTEM_exists(const char* filename)
{
return PHYSFS_exists(fname);
return PHYSFS_exists(filename);
}

static void generateBase36(char* string, const size_t string_size)
Expand Down
92 changes: 91 additions & 1 deletion desktop_version/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,17 @@ void Game::init(void)
ndmresultcrewrescued = 0;
ndmresulttrinkets = 0;

customcol=0;
customcol = 0;

map.currentregion = 0;
for (size_t i = 0; i < SDL_arraysize(map.region); i++)
{
map.region[i].isvalid = false;
map.region[i].rx = 0;
map.region[i].ry = 0;
map.region[i].rx2 = 0;
map.region[i].ry2 = 0;
}

SDL_memset(crewstats, false, sizeof(crewstats));
SDL_memset(ndmresultcrewstats, false, sizeof(ndmresultcrewstats));
Expand Down Expand Up @@ -5580,6 +5590,50 @@ void Game::customloadquick(const std::string& savfile)
map.roomnameset = true;
map.roomname_special = true;
}
else if (SDL_strcmp(pKey, "currentregion") == 0)
{
map.currentregion = help.Int(pText);
}
#if !defined(NO_CUSTOM_LEVELS)
else if (SDL_strcmp(pKey, "regions") == 0)
{
tinyxml2::XMLElement* pElem2;
for (pElem2 = pElem->FirstChildElement(); pElem2 != NULL; pElem2 = pElem2->NextSiblingElement())
{
int thisid = 0;
int thisrx = 0;
int thisry = 0;
int thisrx2 = (cl.mapwidth - 1);
int thisry2 = (cl.mapheight - 1);
if (pElem2->Attribute("id"))
{
thisid = help.Int(pElem2->Attribute("id"));
}

for (tinyxml2::XMLElement* pElem3 = pElem2->FirstChildElement(); pElem3 != NULL; pElem3 = pElem3->NextSiblingElement())
{
if (SDL_strcmp(pElem3->Value(), "rx") == 0 && pElem3->GetText() != NULL)
{
thisrx = help.Int(pElem3->GetText());
}
if (SDL_strcmp(pElem3->Value(), "ry") == 0 && pElem3->GetText() != NULL)
{
thisry = help.Int(pElem3->GetText());
}
if (SDL_strcmp(pElem3->Value(), "rx2") == 0 && pElem3->GetText() != NULL)
{
thisrx2 = help.Int(pElem3->GetText());
}
if (SDL_strcmp(pElem3->Value(), "ry2") == 0 && pElem3->GetText() != NULL)
{
thisry2 = help.Int(pElem3->GetText());
}
}

map.setregion(thisid, thisrx, thisry, thisrx2, thisry2);
}
}
#endif
}
}

Expand Down Expand Up @@ -5871,6 +5925,42 @@ std::string Game::writemaingamesave(tinyxml2::XMLDocument& doc)

xml::update_tag(msgs, "trinkets", trinkets());

xml::update_tag(msgs, "currentregion", map.currentregion);

tinyxml2::XMLElement* msg = xml::update_element_delete_contents(msgs, "regions");
for (size_t i = 0; i < SDL_arraysize(map.region); i++)
{
if (map.region[i].isvalid)
{
tinyxml2::XMLElement* region_el;
region_el = doc.NewElement("region");

region_el->SetAttribute("id", (help.String(i).c_str()));

tinyxml2::XMLElement* rx_el;
rx_el = doc.NewElement("rx");
rx_el->LinkEndChild(doc.NewText(help.String(map.region[i].rx).c_str()));
region_el->LinkEndChild(rx_el);

tinyxml2::XMLElement* ry_el;
ry_el = doc.NewElement("ry");
ry_el->LinkEndChild(doc.NewText(help.String(map.region[i].ry).c_str()));
region_el->LinkEndChild(ry_el);

tinyxml2::XMLElement* rx2_el;
rx2_el = doc.NewElement("rx2");
rx2_el->LinkEndChild(doc.NewText(help.String(map.region[i].rx2).c_str()));
region_el->LinkEndChild(rx2_el);

tinyxml2::XMLElement* ry2_el;
ry2_el = doc.NewElement("ry2");
ry2_el->LinkEndChild(doc.NewText(help.String(map.region[i].ry2).c_str()));
region_el->LinkEndChild(ry2_el);

msg->LinkEndChild(region_el);
}
}


//Special stats

Expand Down
9 changes: 9 additions & 0 deletions desktop_version/src/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,15 @@ void Graphics::drawpartimage(const int t, const int xp, const int yp, const int
draw_texture_part(images[t], xp, yp, 0, 0, wp, hp, 1, 1);
}

void Graphics::draw_region_image(int t, int xp, int yp, int wp, int hp)
{
if (!INBOUNDS_ARR(t, customminimaps) || customminimaps[t] == NULL)
{
return;
}
draw_texture_part(customminimaps[t], xp, yp, 0, 0, wp, hp, 1, 1);
}

void Graphics::draw_texture(SDL_Texture* image, const int x, const int y)
{
int w, h;
Expand Down
4 changes: 4 additions & 0 deletions desktop_version/src/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ class Graphics

void drawpartimage(int t, int xp, int yp, int wp, int hp);

void draw_region_image(int t, int xp, int yp, int wp, int hp);

void drawimage(int t, int xp, int yp, bool cent=false);

void drawimagecol(int t, int xp, int yp, SDL_Color ct, bool cent= false);
Expand Down Expand Up @@ -314,6 +316,8 @@ class Graphics

SDL_Texture* images[NUM_IMAGES];

SDL_Texture* customminimaps[401];

bool flipmode;
bool setflipmode;
bool notextoutline;
Expand Down
36 changes: 34 additions & 2 deletions desktop_version/src/GraphicsResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Alloc.h"
#include "FileSystemUtils.h"
#include "Graphics.h"
#include "GraphicsUtil.h"
#include "Vlogging.h"
#include "Screen.h"
Expand All @@ -21,7 +22,9 @@ extern "C"

static SDL_Surface* LoadImageRaw(const char* filename, unsigned char** data)
{
//Temporary storage for the image that's loaded
*data = NULL;

// Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;

unsigned int width, height;
Expand Down Expand Up @@ -84,7 +87,6 @@ SDL_Surface* LoadImageSurface(const char* filename)

if (optimizedImage == NULL)
{
VVV_free(data);
vlog_error("Image not found: %s", filename);
SDL_assert(0 && "Image not found! See stderr.");
}
Expand Down Expand Up @@ -292,6 +294,28 @@ void GraphicsResources::init(void)
SDL_assert(0 && "Failed to create minimap texture! See stderr.");
return;
}

SDL_zeroa(graphics.customminimaps);

EnumHandle handle = {};
const char* item;
char full_item[73];
while ((item = FILESYSTEM_enumerateAssets("graphics", &handle)) != NULL)
{
if (SDL_strncmp(item, "region", 6) != 0)
{
continue;
}
char* end;
int i = SDL_strtol(&item[6], &end, 10);
if (item == end || SDL_strcmp(end, ".png") != 0)
{
continue;
}
SDL_snprintf(full_item, sizeof(full_item), "graphics/%s", item);
graphics.customminimaps[i] = LoadImage(full_item);
}
FILESYSTEM_freeEnumerate(&handle);
}


Expand Down Expand Up @@ -323,6 +347,14 @@ void GraphicsResources::destroy(void)
CLEAR(im_image10);
CLEAR(im_image11);
CLEAR(im_image12);

for (size_t i = 0; i < SDL_arraysize(graphics.customminimaps); i++)
{
if (graphics.customminimaps[i] != NULL)
{
CLEAR(graphics.customminimaps[i]);
}
}
#undef CLEAR

VVV_freefunc(SDL_FreeSurface, im_sprites_surf);
Expand Down
Loading

0 comments on commit bf8ca30

Please sign in to comment.