Skip to content

Commit

Permalink
Add deleteTexture
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinShadewing committed Oct 27, 2023
1 parent 099e8be commit b0e489a
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 6 deletions.
24 changes: 24 additions & 0 deletions rte/src/api/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ int findTexture(const std::string& name) {
return 0;
}

void deleteTexture(int tex) {
xyDeleteImage(tex);
}

std::string getTextureName(int texture) {
if (texture > 0 && texture < vcTextureNames.size()) {
return vcTextureNames[texture];
Expand All @@ -199,6 +203,26 @@ std::string getTextureName(int texture) {
return "";
}

int getTextureFilter(int tex) {
SDL_ScaleMode sm = (SDL_ScaleMode)0;

if(tex > 0 && tex < vcTextures.size() && vcTextures[tex] != 0)
SDL_GetTextureScaleMode(vcTextures[tex], &sm);
else
xyPrint("WARNING: Texture not found!");

return (int)sm;
}

void setTextureFilter(int tex, int filter) {
SDL_ScaleMode sm = (SDL_ScaleMode)min(max(filter, 0), 2);

if(tex > 0 && tex < vcTextures.size() && vcTextures[tex] != 0)
SDL_SetTextureScaleMode(vcTextures[tex], sm);
else
xyPrint("WARNING: Texture not found!");
}

void printTextureNames() {
for (int i = 0; i < vcTextureNames.size(); i++) {
xyPrint("%d - %s", i, vcTextureNames[i].c_str());
Expand Down
3 changes: 3 additions & 0 deletions rte/src/api/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ int windowH();
int newTexture(int w, int h); // Doc'd
void textureSetBlendMode(int texture, int blend); // Doc'd
int findTexture(const std::string& name);
void deleteTexture(int tex);
std::string getTextureName(int texture);
int getTextureFilter(int tex);
void setTextureFilter(int tex, int filter);
void printTextureNames();

} // namespace BruxAPI
Expand Down
2 changes: 1 addition & 1 deletion rte/src/brux/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Uint32 gvFrames = 0;

// Brux version string

const char *gvVNo = "v0.3.5";
const char *gvVNo = "v0.3.6";

// Should it clear the screen? If true, then the answer is yes.

Expand Down
2 changes: 1 addition & 1 deletion rte/src/brux/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ Uint32 xyNewTexture(Uint32 w, Uint32 h) {

// Return the texture index

vcTextureNames.push_back("new-texture-" + std::to_string(vcTextures.size() - 1));
vcTextureNames.push_back("new-texture-" + std::to_string(vcTextures.size()));
vcTextures.push_back(nimg);

return vcTextures.size() - 1;
Expand Down
8 changes: 4 additions & 4 deletions rte/src/brux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ int main(int argc, char* argv[]) {

// Handle situations where a main file can't be found

if (!shouldLoad) {
puts("ERROR: Unable to load the main file. Make sure that it exists.");
return 1;
}
// if (!shouldLoad) {
// puts("ERROR: Unable to load the main file. Make sure that it exists.");
// return 1;
// }

// Initialize everything

Expand Down
96 changes: 96 additions & 0 deletions rte/src/squirrel/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,29 @@ static SQInteger findTexture_wrapper(HSQUIRRELVM vm)

}

static SQInteger deleteTexture_wrapper(HSQUIRRELVM vm)
{
SQInteger arg0;
if(SQ_FAILED(sq_getinteger(vm, 2, &arg0))) {
sq_throwerror(vm, _SC("Argument 1 not an integer"));
return SQ_ERROR;
}

try {
BruxAPI::deleteTexture(static_cast<int> (arg0));

return 0;

} catch(std::exception& e) {
sq_throwerror(vm, e.what());
return SQ_ERROR;
} catch(...) {
sq_throwerror(vm, _SC("Unexpected exception while executing function 'deleteTexture'"));
return SQ_ERROR;
}

}

static SQInteger getTextureName_wrapper(HSQUIRRELVM vm)
{
SQInteger arg0;
Expand All @@ -1533,6 +1556,58 @@ static SQInteger getTextureName_wrapper(HSQUIRRELVM vm)

}

static SQInteger getTextureFilter_wrapper(HSQUIRRELVM vm)
{
SQInteger arg0;
if(SQ_FAILED(sq_getinteger(vm, 2, &arg0))) {
sq_throwerror(vm, _SC("Argument 1 not an integer"));
return SQ_ERROR;
}

try {
int return_value = BruxAPI::getTextureFilter(static_cast<int> (arg0));

sq_pushinteger(vm, return_value);
return 1;

} catch(std::exception& e) {
sq_throwerror(vm, e.what());
return SQ_ERROR;
} catch(...) {
sq_throwerror(vm, _SC("Unexpected exception while executing function 'getTextureFilter'"));
return SQ_ERROR;
}

}

static SQInteger setTextureFilter_wrapper(HSQUIRRELVM vm)
{
SQInteger arg0;
if(SQ_FAILED(sq_getinteger(vm, 2, &arg0))) {
sq_throwerror(vm, _SC("Argument 1 not an integer"));
return SQ_ERROR;
}
SQInteger arg1;
if(SQ_FAILED(sq_getinteger(vm, 3, &arg1))) {
sq_throwerror(vm, _SC("Argument 2 not an integer"));
return SQ_ERROR;
}

try {
BruxAPI::setTextureFilter(static_cast<int> (arg0), static_cast<int> (arg1));

return 0;

} catch(std::exception& e) {
sq_throwerror(vm, e.what());
return SQ_ERROR;
} catch(...) {
sq_throwerror(vm, _SC("Unexpected exception while executing function 'setTextureFilter'"));
return SQ_ERROR;
}

}

static SQInteger printTextureNames_wrapper(HSQUIRRELVM vm)
{
(void) vm;
Expand Down Expand Up @@ -4536,13 +4611,34 @@ void register_brux_wrapper(HSQUIRRELVM v)
throw SquirrelError(v, "Couldn't register function 'findTexture'");
}

sq_pushstring(v, "deleteTexture", -1);
sq_newclosure(v, &deleteTexture_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".b|n");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'deleteTexture'");
}

sq_pushstring(v, "getTextureName", -1);
sq_newclosure(v, &getTextureName_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".b|n");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'getTextureName'");
}

sq_pushstring(v, "getTextureFilter", -1);
sq_newclosure(v, &getTextureFilter_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".b|n");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'getTextureFilter'");
}

sq_pushstring(v, "setTextureFilter", -1);
sq_newclosure(v, &setTextureFilter_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".b|nb|n");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'setTextureFilter'");
}

sq_pushstring(v, "printTextureNames", -1);
sq_newclosure(v, &printTextureNames_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".");
Expand Down

0 comments on commit b0e489a

Please sign in to comment.