Added readFile and writeFile utility functions and changeConfig member function
This commit is contained in:
parent
9bfbfb637b
commit
22deb5a7ad
@ -3,4 +3,20 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<ShowAllFiles>true</ShowAllFiles>
|
<ShowAllFiles>true</ShowAllFiles>
|
||||||
</PropertyGroup>
|
</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>
|
</Project>
|
@ -18,8 +18,10 @@ namespace nf {
|
|||||||
m_hInst = GetModuleHandle(NULL);
|
m_hInst = GetModuleHandle(NULL);
|
||||||
registerWindowClass();
|
registerWindowClass();
|
||||||
RECT windowSize = getWindowRect();
|
RECT windowSize = getWindowRect();
|
||||||
|
int x = 0;
|
||||||
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 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);
|
m_defaultWindowStyle = GetWindowLong(m_window, GWL_STYLE);
|
||||||
SetProp(m_window, L"App", this);
|
SetProp(m_window, L"App", this);
|
||||||
if (m_currentConfig.fullscreen) toggleFullscreen();
|
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() {
|
void Application::run() {
|
||||||
addIntroState();
|
addIntroState();
|
||||||
showWindow(true);
|
showWindow(true);
|
||||||
@ -95,6 +86,31 @@ namespace nf {
|
|||||||
ShowWindow(m_window, SW_HIDE);
|
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 {
|
const Config& Application::getConfig() const {
|
||||||
return m_currentConfig;
|
return m_currentConfig;
|
||||||
}
|
}
|
||||||
@ -175,7 +191,7 @@ namespace nf {
|
|||||||
else {
|
else {
|
||||||
SetWindowLong(m_window, GWL_STYLE, m_defaultWindowStyle);
|
SetWindowLong(m_window, GWL_STYLE, m_defaultWindowStyle);
|
||||||
SetWindowPlacement(m_window, &m_wndPlacement);
|
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;
|
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) {
|
LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
|
||||||
Application* app = (Application*)GetProp(hWnd, L"App");
|
Application* app = (Application*)GetProp(hWnd, L"App");
|
||||||
//TODO: Dragging blocks thread
|
|
||||||
switch (uMsg) {
|
switch (uMsg) {
|
||||||
case WM_CREATE: {
|
case WM_CREATE: {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
case WM_SYSKEYDOWN: {
|
case WM_SYSKEYDOWN: {
|
||||||
if (GetKeyState(VK_RETURN) & 0x8000) {
|
if (GetKeyState(VK_RETURN) & 0x8000) {
|
||||||
|
app->m_currentConfig.fullscreen = !app->m_currentConfig.fullscreen;
|
||||||
app->toggleFullscreen();
|
app->toggleFullscreen();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return 0;
|
break;
|
||||||
}
|
}
|
||||||
case WM_MENUCHAR: {
|
case WM_MENUCHAR: {
|
||||||
return MNC_CLOSE << 16;
|
return MNC_CLOSE << 16;
|
||||||
@ -253,7 +279,8 @@ namespace nf {
|
|||||||
return 0;
|
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() {
|
void Application::createOpenGLContext() {
|
||||||
@ -303,6 +330,10 @@ namespace nf {
|
|||||||
|
|
||||||
Application::~Application() {
|
Application::~Application() {
|
||||||
Log("Exiting NF 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
//TODO: Debug logger
|
|
||||||
//TODO: File IO functions
|
//TODO: File IO functions
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
@ -48,6 +47,30 @@ namespace nf {
|
|||||||
MultiByteToWideChar(CP_ACP, NULL, in, -1, out, length);
|
MultiByteToWideChar(CP_ACP, NULL, in, -1, out, length);
|
||||||
return out;
|
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
|
//Nvidia Optimius support
|
||||||
|
@ -19,9 +19,10 @@ namespace nf {
|
|||||||
void setWindowCursor(HCURSOR hCursor);
|
void setWindowCursor(HCURSOR hCursor);
|
||||||
void addState(IGamestate* state, const char* stateName);
|
void addState(IGamestate* state, const char* stateName);
|
||||||
void addDefaultState(const char* stateName);
|
void addDefaultState(const char* stateName);
|
||||||
void changeState(const char* stateName);
|
|
||||||
void run();
|
void run();
|
||||||
void showWindow(bool show);
|
void showWindow(bool show);
|
||||||
|
void changeState(const char* stateName);
|
||||||
|
void changeConfig(const Config& in);
|
||||||
const Config& getConfig() const;
|
const Config& getConfig() const;
|
||||||
int getFPS() const;
|
int getFPS() const;
|
||||||
bool isInput(unsigned int code);
|
bool isInput(unsigned int code);
|
||||||
@ -33,6 +34,7 @@ namespace nf {
|
|||||||
void registerWindowClass();
|
void registerWindowClass();
|
||||||
void toggleFullscreen();
|
void toggleFullscreen();
|
||||||
RECT getWindowRect() const;
|
RECT getWindowRect() const;
|
||||||
|
void calculateNewWindowPos(int& x, int& y);
|
||||||
|
|
||||||
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace nf {
|
namespace nf {
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
@ -39,4 +41,6 @@ std::exit(-1)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
const wchar_t* toWide(const char* in);
|
const wchar_t* toWide(const char* in);
|
||||||
|
bool writeFile(const char* filename, const std::string& in);
|
||||||
|
std::string readFile(const char* filename);
|
||||||
}
|
}
|
@ -17,11 +17,10 @@ Remember to use tasks (//TODO: )
|
|||||||
*Namespaced
|
*Namespaced
|
||||||
*Debug and log system
|
*Debug and log system
|
||||||
*NatVis
|
*NatVis
|
||||||
Video options
|
Config changing
|
||||||
*Alt-Enter
|
*Alt-Enter
|
||||||
File IO functions
|
*File IO functions
|
||||||
Options file (Executable directory)
|
Input (mouse position)
|
||||||
Input
|
|
||||||
Audio
|
Audio
|
||||||
*Game states
|
*Game states
|
||||||
Text rendering
|
Text rendering
|
||||||
|
Reference in New Issue
Block a user