Skip to content

Commit

Permalink
Fixced the mouse position bug for real this time
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkyun991 committed Jan 18, 2020
1 parent 83df623 commit 7fea97d
Show file tree
Hide file tree
Showing 25 changed files with 97 additions and 80 deletions.
6 changes: 5 additions & 1 deletion Empaerior/Empaerior.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>..\SDL2\lib\x86;..\SDL2_image\lib\x86;..\SDL2_mixer\lib\x86;..\SDL2_ttf\lib\x86;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
Expand Down Expand Up @@ -218,4 +218,8 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<ItemGroup>
<ResourceCompile Include="icon.rc">
</ResourceCompile>
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions Empaerior/Empaerior.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,7 @@
<Filter>utilities</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="icon.rc" />
</ItemGroup>
</Project>
84 changes: 15 additions & 69 deletions Empaerior/Src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Game.h"
#include <SDL.h>
#include "utilities/collisions.h"
#include "utilities/Utilities.h"
#include <SDL_mouse.h>
#include <random>
//for testing
Expand Down Expand Up @@ -89,7 +90,7 @@ State::State()
std::uniform_int_distribution<int> yDist(0, 15);
std::uniform_int_distribution<int> mines_rng(lower_bound, upper_bound);
int mines = mines_rng(rng);
std::cout << "mines: " << mines << '\n';

while (mines != 0)
{
field_matrix[xDist( rng)][yDist( rng)] = -1;
Expand Down Expand Up @@ -178,30 +179,11 @@ State::State()
{

//mouse coordinates
int m_x = 0;
int m_y = 0;
SDL_GetMouseState(&m_x, &m_y);



m_x *= kamera.rect.w;
m_y *= kamera.rect.h;



m_x /= 960;
m_y /= 800;

m_x += kamera.rect.x;
m_y += kamera.rect.y;


SDL_Rect renderer_viewport;
SDL_RenderGetViewport(Empaerior::Game::window.renderer, &renderer_viewport);


m_x += renderer_viewport.x;
m_y += renderer_viewport.y;
auto f_m = Empaerior::get_world_mouse_coords(kamera);

int m_x = int(f_m.first);
int m_y = int(f_m.second);



if (rect_contains_point(Ecs.get_component<Empaerior::Sprite_Component>(face_boi_id).sprites[0].get_dimensions(), m_x, m_y))
Expand Down Expand Up @@ -230,29 +212,11 @@ State::State()
{

//mouse coordinates
int m_x = 0;
int m_y = 0;
SDL_GetMouseState(&m_x, &m_y);



m_x *= kamera.rect.w;
m_y *= kamera.rect.h;



m_x /= 960;
m_y /= 800;

m_x += kamera.rect.x;
m_y += kamera.rect.y;

SDL_Rect renderer_viewport;
SDL_RenderGetViewport(Empaerior::Game::window.renderer, &renderer_viewport);


auto f_m = Empaerior::get_world_mouse_coords(kamera);

m_x += renderer_viewport.x;
m_y += renderer_viewport.y;
int m_x = int(f_m.first);
int m_y = int(f_m.second);

if (rect_contains_point(Ecs.get_component<Empaerior::Sprite_Component>(face_boi_id).sprites[0].get_dimensions(), m_x, m_y))
{
Expand Down Expand Up @@ -307,29 +271,11 @@ State::State()
#define l_tile Ecs.get_component<Mine_field>(map).field[i][j]

//mouse coordinates
int m_x = 0;
int m_y = 0;
SDL_GetMouseState(&m_x, &m_y);



m_x *= kamera.rect.w;
m_y *= kamera.rect.h;



m_x /= 960;
m_y /= 800;

m_x += kamera.rect.x;
m_y += kamera.rect.y;

SDL_Rect renderer_viewport;
SDL_RenderGetViewport(Empaerior::Game::window.renderer, &renderer_viewport);


auto f_m = Empaerior::get_world_mouse_coords(kamera);

m_x -= renderer_viewport.x;
m_y -= renderer_viewport.y;
int m_x = int(f_m.first);
int m_y = int(f_m.second);



Expand Down
9 changes: 7 additions & 2 deletions Empaerior/Src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include "pch.h"


#include <Windows.h>

#ifdef _DEBUG
#ifndef DBG_NEW
Expand All @@ -13,7 +15,7 @@
//



#include "SDL_Main.h"
#include "Game.h"

#include <crtdbg.h>
Expand Down Expand Up @@ -49,8 +51,11 @@ Empaerior::Window Empaerior::Game::window;



int main(int argc, char** argv)
int SDL_main(int argc, char* argv[])
{
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
#pragma region SDL_Inititalization
Expand Down
58 changes: 57 additions & 1 deletion Empaerior/Src/utilities/Utilities.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
#include "pch.h"
#include "pch.h"
#include "Utilities.h"
#include "Game.h"
std::pair<float, float> Empaerior::get_world_mouse_coords(const Empaerior::Camera& camera)
{
//get the positions
std::pair<int, int> pos;
std::pair<float, float> f_pos;


SDL_GetMouseState(&pos.first, &pos.second);



f_pos.first = float(pos.first);
f_pos.second = float(pos.second);



//Scale down the positions to match the world
float sx = 0;
float sy = 0;
SDL_RenderGetScale(Empaerior::Game::window.renderer, &sx, &sy);

f_pos.first /= sx;
f_pos.second /= sy;




//Transform for the position of the renderer
SDL_Rect renderer_viewport;

SDL_RenderGetViewport(Empaerior::Game::window.renderer, &renderer_viewport);
f_pos.first -= renderer_viewport.x;
f_pos.second -= renderer_viewport.y;

//Transform the position relative to the camera dimesnions
f_pos.first *= camera.rect.w;
f_pos.second *= camera.rect.h;



f_pos.first /= renderer_viewport.w;
f_pos.second /= renderer_viewport.h;



//Tranform for position
f_pos.first += camera.rect.x;
f_pos.second += camera.rect.y;




return f_pos;
}
16 changes: 9 additions & 7 deletions Empaerior/Src/utilities/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#include <SDL.h>
#include <string>
#include <iostream>
#include "graphics/rendering/Camera.h"
#include "Exceptions/Exceptions.h"
namespace Empaerior
{
//clipboard functions
std::string get_clipboard_text()
inline std::string get_clipboard_text()
{
if (SDL_HasClipboardText())//if there's text
{
Expand All @@ -16,7 +18,7 @@ namespace Empaerior
}
return "";
}
void set_clipboard_text(const char* text)
inline void set_clipboard_text(const char* text)
{
try
{
Expand All @@ -35,7 +37,7 @@ namespace Empaerior


//system functions
std::string get_platform()//gets the current platform
inline std::string get_platform()//gets the current platform
{
const char* sdl_platform = SDL_GetPlatform();
std::string e_platform = sdl_platform;
Expand All @@ -44,22 +46,22 @@ namespace Empaerior

}

int cpu_cache_size()//returns the size of the cpu cache in bytes
inline int cpu_cache_size()//returns the size of the cpu cache in bytes
{
return SDL_GetCPUCacheLineSize();
}

int get_core_number()// get the number of CPU cores available
inline int get_core_number()// get the number of CPU cores available
{
return SDL_GetCPUCount();
}

int get_system_ram()//get the amount of RAM configured in the system.
inline int get_system_ram()//get the amount of RAM configured in the system.
{
return SDL_GetSystemRAM();
}


std::pair<float,float> get_world_mouse_coords(const Empaerior::Camera& camera);


}
Binary file not shown.
Binary file modified bin-int/Debug-windows-/Empaerior/Empaerior.tlog/CL.read.1.tlog
Binary file not shown.
Binary file modified bin-int/Debug-windows-/Empaerior/Empaerior.tlog/CL.write.1.tlog
Binary file not shown.
Binary file modified bin-int/Debug-windows-/Empaerior/Empaerior.tlog/link.command.1.tlog
Binary file not shown.
Binary file modified bin-int/Debug-windows-/Empaerior/Empaerior.tlog/link.read.1.tlog
Binary file not shown.
Binary file modified bin-int/Debug-windows-/Empaerior/Empaerior.tlog/link.write.1.tlog
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added bin-int/Debug-windows-/Empaerior/icon.res
Binary file not shown.
Binary file modified bin-int/Debug-windows-/Empaerior/vc141.idb
Binary file not shown.
Binary file modified bin-int/Dist-windows-/Empaerior/Empaerior.tlog/CL.command.1.tlog
Binary file not shown.
Binary file modified bin-int/Dist-windows-/Empaerior/Empaerior.tlog/CL.read.1.tlog
Binary file not shown.
Binary file modified bin-int/Dist-windows-/Empaerior/Empaerior.tlog/CL.write.1.tlog
Binary file not shown.
Binary file modified bin-int/Dist-windows-/Empaerior/Empaerior.tlog/link.command.1.tlog
Binary file not shown.
Binary file modified bin-int/Dist-windows-/Empaerior/Empaerior.tlog/link.read.1.tlog
Binary file not shown.
Binary file modified bin-int/Dist-windows-/Empaerior/Empaerior.tlog/link.write.1.tlog
Binary file not shown.
Binary file modified bin-int/Dist-windows-/Empaerior/vc141.idb
Binary file not shown.
1 change: 1 addition & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
workspace "Empaerior"

architecture "x86"

configurations
Expand Down

0 comments on commit 7fea97d

Please sign in to comment.