Added readFile and writeFile utility functions and changeConfig member function

This commit is contained in:
Grayson Riffe (Laptop) 2021-08-19 16:08:26 -05:00
parent 9bfbfb637b
commit 22deb5a7ad
6 changed files with 99 additions and 24 deletions

View File

@ -3,4 +3,20 @@
<PropertyGroup>
<ShowAllFiles>true</ShowAllFiles>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>

View File

@ -18,8 +18,10 @@ namespace nf {
m_hInst = GetModuleHandle(NULL);
registerWindowClass();
RECT windowSize = getWindowRect();
m_window = CreateWindowEx(NULL, m_wclassName, toWide(m_currentConfig.title), WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 1280, windowSize.bottom, NULL, NULL, m_hInst, NULL);
int x = 0;
int y = 0;
calculateNewWindowPos(x, y);
m_window = CreateWindowEx(NULL, m_wclassName, toWide(m_currentConfig.title), WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, x, y, windowSize.right, windowSize.bottom, NULL, NULL, m_hInst, NULL);
m_defaultWindowStyle = GetWindowLong(m_window, GWL_STYLE);
SetProp(m_window, L"App", this);
if (m_currentConfig.fullscreen) toggleFullscreen();
@ -58,17 +60,6 @@ namespace nf {
}
}
void Application::changeState(const char* stateName) {
if (m_states.find(stateName) != m_states.end()) {
m_currentState->onExit();
m_currentState = m_states[stateName];
m_currentState->onEnter(this);
}
else {
Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str());
}
}
void Application::run() {
addIntroState();
showWindow(true);
@ -95,6 +86,31 @@ namespace nf {
ShowWindow(m_window, SW_HIDE);
}
void Application::changeState(const char* stateName) {
if (m_states.find(stateName) != m_states.end()) {
m_currentState->onExit();
m_currentState = m_states[stateName];
m_currentState->onEnter(this);
}
else {
Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str());
}
}
void Application::changeConfig(const Config& in) {
SetWindowText(m_window, toWide(in.title));
if (in.fullscreen != m_currentConfig.fullscreen) {
m_currentConfig = in;
toggleFullscreen();
}
if (m_currentConfig.fullscreen)
return;
m_currentConfig = in;
int x = 0, y = 0;
calculateNewWindowPos(x, y);
SetWindowPos(m_window, HWND_TOP, x, y, in.width, in.height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
const Config& Application::getConfig() const {
return m_currentConfig;
}
@ -175,7 +191,7 @@ namespace nf {
else {
SetWindowLong(m_window, GWL_STYLE, m_defaultWindowStyle);
SetWindowPlacement(m_window, &m_wndPlacement);
SetWindowPos(m_window, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
SetWindowPos(m_window, NULL, 0, 0, m_currentConfig.width, m_currentConfig.height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
}
@ -191,19 +207,29 @@ namespace nf {
return temp;
}
void Application::calculateNewWindowPos(int& x, int& y) {
MONITORINFO mi = { sizeof(mi) };
GetMonitorInfo(MonitorFromWindow(m_window, MONITOR_DEFAULTTOPRIMARY), &mi);
int monX = (mi.rcWork.right - mi.rcWork.left) / 2;
int monY = (mi.rcWork.bottom - mi.rcWork.top) / 2;
x = monX - (m_currentConfig.width / 2);
y = monY - (m_currentConfig.height / 2);
}
LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
Application* app = (Application*)GetProp(hWnd, L"App");
//TODO: Dragging blocks thread
switch (uMsg) {
case WM_CREATE: {
return 0;
}
case WM_SYSKEYDOWN: {
if (GetKeyState(VK_RETURN) & 0x8000) {
app->m_currentConfig.fullscreen = !app->m_currentConfig.fullscreen;
app->toggleFullscreen();
return 0;
}
return 0;
break;
}
case WM_MENUCHAR: {
return MNC_CLOSE << 16;
@ -253,7 +279,8 @@ namespace nf {
return 0;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);//TODO: Fill out events
return DefWindowProc(hWnd, uMsg, wParam, lParam);
//TODO: Fill out events
}
void Application::createOpenGLContext() {
@ -303,6 +330,10 @@ namespace nf {
Application::~Application() {
Log("Exiting NF application");
//TODO: Iterate through m_activeStates and m_states and exit and unload them
for (std::pair<const char*, IGamestate*> state : m_states) {
IGamestate* curr = state.second;
delete curr;
}
}
}

View File

@ -1,4 +1,3 @@
//TODO: Debug logger
//TODO: File IO functions
#include <Windows.h>
@ -48,6 +47,30 @@ namespace nf {
MultiByteToWideChar(CP_ACP, NULL, in, -1, out, length);
return out;
}
bool writeFile(const char* filename, const std::string& in) {
std::ofstream out;
out.open(filename);
if (!out) {
Error(("File \"" + (std::string)filename + (std::string)"\" could not be written!").c_str());
return false;
}
out << in;
out.close();
return true;
}
std::string readFile(const char* filename) {
std::ifstream in;
in.open(filename);
if (!in) {
Error("Cannot find file");
return NULL;
}
std::stringstream ss;
ss << in.rdbuf();
return ss.str();
}
}
//Nvidia Optimius support

View File

@ -19,9 +19,10 @@ namespace nf {
void setWindowCursor(HCURSOR hCursor);
void addState(IGamestate* state, const char* stateName);
void addDefaultState(const char* stateName);
void changeState(const char* stateName);
void run();
void showWindow(bool show);
void changeState(const char* stateName);
void changeConfig(const Config& in);
const Config& getConfig() const;
int getFPS() const;
bool isInput(unsigned int code);
@ -33,6 +34,7 @@ namespace nf {
void registerWindowClass();
void toggleFullscreen();
RECT getWindowRect() const;
void calculateNewWindowPos(int& x, int& y);
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

View File

@ -3,6 +3,8 @@
#include <thread>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
namespace nf {
#ifdef _DEBUG
@ -39,4 +41,6 @@ std::exit(-1)
#endif
const wchar_t* toWide(const char* in);
bool writeFile(const char* filename, const std::string& in);
std::string readFile(const char* filename);
}

View File

@ -17,11 +17,10 @@ Remember to use tasks (//TODO: )
*Namespaced
*Debug and log system
*NatVis
Video options
Config changing
*Alt-Enter
File IO functions
Options file (Executable directory)
Input
*File IO functions
Input (mouse position)
Audio
*Game states
Text rendering