Skip to content

Commit

Permalink
Switch scaling method
Browse files Browse the repository at this point in the history
  • Loading branch information
sago007 committed Oct 18, 2024
1 parent 81a1a1d commit 172ff70
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
8 changes: 2 additions & 6 deletions src/saland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,7 @@ void RunGameState(sago::GameStateInterface& state ) {
ImGui::Render();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = NULL;
//float x, y;
//SDL_RenderGetScale(globalData.screen, &x, &y);
//SDL_RenderSetScale(globalData.screen, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
ImGui_ImplSDLRenderer2_RenderDrawData( ImGui::GetDrawData(), globalData.screen );
//SDL_RenderSetScale(globalData.screen, x, y);

//While using Dear ImGui we do not draw the mouse ourself. This is gone: globalData.mouse.Draw(globalData.screen, SDL_GetTicks(), globalData.mousex, globalData.mousey);
SDL_RenderPresent(globalData.screen);
Expand All @@ -243,7 +239,7 @@ void RunGameState(sago::GameStateInterface& state ) {
if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
std::cout << event.window.data1 << ", " << event.window.data2 << "\n";
//SDL_GetRendererOutputSize(globalData.screen, &globalData.xsize, &globalData.ysize);
SDL_GetRendererOutputSize(globalData.screen, &globalData.xsize, &globalData.ysize);
}
}

Expand Down Expand Up @@ -436,7 +432,7 @@ void runGame() {
win = SDL_CreateWindow("Saland Adventures", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, globalData.xsize, globalData.ysize, SDL_WINDOW_RESIZABLE);
globalData.screen = SDL_CreateRenderer(win, -1, rendererFlags);

SDL_RenderSetLogicalSize(globalData.screen, globalData.xsize, globalData.ysize);
//SDL_RenderSetLogicalSize(globalData.screen, globalData.xsize, globalData.ysize);
InitImGui(win, globalData.screen, globalData.xsize, globalData.ysize);
globalData.ysize = globalData.ysize;

Expand Down
45 changes: 29 additions & 16 deletions src/saland/GameDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,33 @@ sago::SagoTextField* TextCache::getLabel(const std::string& text) {

TextCache textCache;

static float scale = 1.0f;

static int getScaledX(int x) {
return x * scale;
}
static int getScaledY(int y) {
return y * scale;
}

static void Draw(SDL_Renderer* target, SDL_Texture* t, int x, int y, const SDL_Rect& part) {
SDL_Rect pos = {};
pos.x = x;
pos.y = y;
pos.w = 32;
pos.h = 32;
pos.x = getScaledX(x);
pos.y = getScaledY(y);
pos.w = getScaledX(32+x)-pos.x;
pos.h = getScaledY(32+y)-pos.y;
SDL_RenderCopy(target, t, &part, &pos);
}

static void DrawCollision(SDL_Renderer* target, const Placeable* entity, int offsetX, int offsetY, bool drawCollision) {
if (drawCollision) {
int x = getScaledX(entity->X - offsetX);
int y = getScaledY(entity->Y - offsetY);
int r = getScaledX(entity->Radius);
circleRGBA(target,
entity->X - offsetX, entity->Y - offsetY, entity->Radius,
x, y, r,
255, 255, 0, 255);
textCache.getLabel(entity->id)->Draw(target, entity->X - offsetX + entity->Radius + 4, entity->Y - offsetY, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center);
textCache.getLabel(entity->id)->Draw(target, x + r + 4, y, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::center);
}
}

Expand Down Expand Up @@ -170,30 +181,32 @@ void DrawHumanEntity(SDL_Renderer* target, sago::SagoSpriteHolder* sHolder, cons
relativeAnimation = true;
relativeAnimationState = 0.9f;
}
int x = getScaledX(std::round(entity->X) - offsetX);
int y = getScaledY(std::round(entity->Y) - offsetY);
DrawCollision(target, entity, offsetX, offsetY, drawCollision);
const sago::SagoSprite& mySprite = sHolder->GetSprite(entity->race + "_" + animation + "_" + std::string(1, entity->direction));
if (relativeAnimation) {
mySprite.DrawProgressive(target, relativeAnimationState, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
mySprite.DrawProgressive(target, relativeAnimationState, x, y);
}
else {
mySprite.Draw(target, time, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
mySprite.Draw(target, time, x, y);
}
if (entity->pants.length() > 0) {
const sago::SagoSprite& myPants = sHolder->GetSprite(entity->race + "_"+animation+"_"+entity->pants+"_"+std::string(1,entity->direction));
if (relativeAnimation) {
myPants.DrawProgressive(target, relativeAnimationState, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
myPants.DrawProgressive(target, relativeAnimationState, x, y);
}
else {
myPants.Draw(target, time, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
myPants.Draw(target, time, x, y);
}
}
if (entity->top.length() > 0) {
const sago::SagoSprite& myTop = sHolder->GetSprite(entity->race + "_"+animation+"_"+entity->top+"_"+std::string(1,entity->direction));
if (relativeAnimation) {
myTop.DrawProgressive(target, relativeAnimationState, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
myTop.DrawProgressive(target, relativeAnimationState, x, y);
}
else {
myTop.Draw(target, time, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
myTop.Draw(target, time, x, y);
}
}
if (entity->hair.length() > 0) {
Expand All @@ -203,19 +216,19 @@ void DrawHumanEntity(SDL_Renderer* target, sago::SagoSpriteHolder* sHolder, cons
}
const sago::SagoSprite& myHair = sHolder->GetSprite(entity->race + "_"+hairAnimation+"_"+entity->hair+"_"+std::string(1,entity->direction));
if (relativeAnimation) {
myHair.DrawProgressive(target, relativeAnimationState, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
myHair.DrawProgressive(target, relativeAnimationState, x, y);
}
else {
myHair.Draw(target, time, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
myHair.Draw(target, time, x, y);
}
}
if (entity->weapon.length() > 0) {
const sago::SagoSprite& myWeapon = sHolder->GetSprite("human_"+animation+"_"+entity->weapon+"_"+std::string(1,entity->direction));
if (relativeAnimation) {
myWeapon.DrawProgressive(target, relativeAnimationState, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
myWeapon.DrawProgressive(target, relativeAnimationState, x, y);
}
else {
myWeapon.Draw(target, time, std::round(entity->X) - offsetX, std::round(entity->Y) - offsetY);
myWeapon.Draw(target, time, x, y);
}
}
}
Expand Down

0 comments on commit 172ff70

Please sign in to comment.