about summary refs log tree commit diff stats
path: root/Source
diff options
context:
space:
mode:
authorjbzdarkid <jbzdarkid@gmail.com>2018-10-30 20:00:32 -0700
committerjbzdarkid <jbzdarkid@gmail.com>2018-10-30 20:00:32 -0700
commite66a408e38849107a3d35a317ff6f2b23dcaeead (patch)
tree2e2abb9c80997c0565fddce4e7e271d901839b26 /Source
parent2ec4950107245df8f01860a3289b7731753933cd (diff)
downloadwitness-tutorializer-e66a408e38849107a3d35a317ff6f2b23dcaeead.tar.gz
witness-tutorializer-e66a408e38849107a3d35a317ff6f2b23dcaeead.tar.bz2
witness-tutorializer-e66a408e38849107a3d35a317ff6f2b23dcaeead.zip
RM static cast, lower warning level, add homebrew RNG
Diffstat (limited to 'Source')
-rw-r--r--Source/Main.cpp16
-rw-r--r--Source/Memory.cpp4
-rw-r--r--Source/Random.h17
-rw-r--r--Source/Randomizer.cpp2
-rw-r--r--Source/RandomizerCore.cpp3
-rw-r--r--Source/Source.vcxproj3
-rw-r--r--Source/Source.vcxproj.filters3
7 files changed, 37 insertions, 11 deletions
diff --git a/Source/Main.cpp b/Source/Main.cpp index e6b149f..28bc472 100644 --- a/Source/Main.cpp +++ b/Source/Main.cpp
@@ -1,10 +1,13 @@
1#include "windows.h"
1#include "resource.h" 2#include "resource.h"
2#include <Richedit.h> 3#include <Richedit.h>
4
3#include <ctime> 5#include <ctime>
4#include <string> 6#include <string>
7
5#include "Randomizer.h" 8#include "Randomizer.h"
6#include "windows.h"
7#include "Version.h" 9#include "Version.h"
10#include "Random.h"
8 11
9#define IDC_RANDOMIZE 0x401 12#define IDC_RANDOMIZE 0x401
10#define IDC_TOGGLESPEED 0x402 13#define IDC_TOGGLESPEED 0x402
@@ -37,9 +40,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
37 GetWindowText(hwndSeed, &text[0], 100); 40 GetWindowText(hwndSeed, &text[0], 100);
38 int seed = 0; 41 int seed = 0;
39 if (wasSeedRandomlyGenerated || wcslen(text.c_str()) == 0) { 42 if (wasSeedRandomlyGenerated || wcslen(text.c_str()) == 0) {
40 srand(static_cast<unsigned int>(time(nullptr))); // Seed from the time in milliseconds 43 Random::SetSeed(time(nullptr)); // Seed from the time in milliseconds
41 rand(); // Increment RNG so that RNG isn't still using the time 44 seed = Random::RandInt(0, 100000);
42 seed = rand() % 100000;
43 std::wstring seedString = std::to_wstring(seed); 45 std::wstring seedString = std::to_wstring(seed);
44 SetWindowText(hwndSeed, seedString.c_str()); 46 SetWindowText(hwndSeed, seedString.c_str());
45 wasSeedRandomlyGenerated = true; 47 wasSeedRandomlyGenerated = true;
@@ -47,7 +49,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
47 seed = _wtoi(text.c_str()); 49 seed = _wtoi(text.c_str());
48 wasSeedRandomlyGenerated = false; 50 wasSeedRandomlyGenerated = false;
49 } 51 }
50 srand(seed); 52 Random::SetSeed(seed);
51 Randomizer randomizer; 53 Randomizer randomizer;
52 randomizer.Randomize(); 54 randomizer.Randomize();
53 if (IsDlgButtonChecked(hwnd, IDC_TOGGLESPEED)) { 55 if (IsDlgButtonChecked(hwnd, IDC_TOGGLESPEED)) {
@@ -79,8 +81,10 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
79 }; 81 };
80 RegisterClassW(&wndClass); 82 RegisterClassW(&wndClass);
81 83
84 RECT rect;
85 GetClientRect(GetDesktopWindow(), &rect);
82 HWND hwnd = CreateWindow(WINDOW_CLASS, PRODUCT_NAME, WS_OVERLAPPEDWINDOW, 86 HWND hwnd = CreateWindow(WINDOW_CLASS, PRODUCT_NAME, WS_OVERLAPPEDWINDOW,
83 400, 200, 500, 500, nullptr, nullptr, hInstance, nullptr); 87 rect.right - 550, 200, 500, 500, nullptr, nullptr, hInstance, nullptr);
84 88
85 CreateWindow(L"STATIC", L"Version: " VERSION_STR, 89 CreateWindow(L"STATIC", L"Version: " VERSION_STR,
86 WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, 90 WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT,
diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 0afeded..168b4e2 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp
@@ -25,7 +25,7 @@ Memory::Memory(const std::string& processName) {
25 // Next, get the process base address 25 // Next, get the process base address
26 DWORD numModules; 26 DWORD numModules;
27 std::vector<HMODULE> moduleList(1024); 27 std::vector<HMODULE> moduleList(1024);
28 EnumProcessModulesEx(_handle, &moduleList[0], static_cast<DWORD>(moduleList.size()), &numModules, 3); 28 EnumProcessModulesEx(_handle, &moduleList[0], moduleList.size(), &numModules, 3);
29 29
30 std::string name(64, 0); 30 std::string name(64, 0);
31 for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) { 31 for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) {
@@ -49,7 +49,7 @@ Memory::~Memory() {
49 49
50void Memory::ThrowError() { 50void Memory::ThrowError() {
51 std::string message(256, '\0'); 51 std::string message(256, '\0');
52 FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], static_cast<DWORD>(message.length()), nullptr); 52 FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], message.length(), nullptr);
53 std::cout << message.c_str() << std::endl; 53 std::cout << message.c_str() << std::endl;
54 exit(EXIT_FAILURE); 54 exit(EXIT_FAILURE);
55} 55}
diff --git a/Source/Random.h b/Source/Random.h new file mode 100644 index 0000000..e4700f3 --- /dev/null +++ b/Source/Random.h
@@ -0,0 +1,17 @@
1#pragma once
2
3static int s_seed;
4
5class Random
6{
7public:
8 static void SetSeed(int seed) {
9 s_seed = seed;
10 }
11
12 static int RandInt(int min, int max) {
13 s_seed = (214013 * s_seed + 2531011) % 2147483648;
14 if (min == max) return min;
15 return (s_seed % (max - (min - 1))) + min;
16 }
17};
diff --git a/Source/Randomizer.cpp b/Source/Randomizer.cpp index c3c084c..83f5571 100644 --- a/Source/Randomizer.cpp +++ b/Source/Randomizer.cpp
@@ -23,7 +23,7 @@
23template <class T> 23template <class T>
24int find(const std::vector<T> &data, T search, size_t startIndex = 0) { 24int find(const std::vector<T> &data, T search, size_t startIndex = 0) {
25 for (size_t i=startIndex ; i<data.size(); i++) { 25 for (size_t i=startIndex ; i<data.size(); i++) {
26 if (data[i] == search) return static_cast<int>(i); 26 if (data[i] == search) return i;
27 } 27 }
28 std::cout << "Couldn't find " << search << " in data!" << std::endl; 28 std::cout << "Couldn't find " << search << " in data!" << std::endl;
29 exit(-1); 29 exit(-1);
diff --git a/Source/RandomizerCore.cpp b/Source/RandomizerCore.cpp index 77977c6..684e19d 100644 --- a/Source/RandomizerCore.cpp +++ b/Source/RandomizerCore.cpp
@@ -1,5 +1,6 @@
1#include "RandomizerCore.h" 1#include "RandomizerCore.h"
2#include "Memory.h" 2#include "Memory.h"
3#include "Random.h"
3#include <sstream> 4#include <sstream>
4 5
5void RandomizerCore::Randomize(std::vector<int>& panels, int flags) { 6void RandomizerCore::Randomize(std::vector<int>& panels, int flags) {
@@ -12,7 +13,7 @@ void RandomizerCore::RandomizeRange(std::vector<int> &panels, int flags, size_t
12 if (startIndex >= endIndex) return; 13 if (startIndex >= endIndex) return;
13 if (endIndex >= panels.size()) endIndex = panels.size(); 14 if (endIndex >= panels.size()) endIndex = panels.size();
14 for (size_t i = endIndex-1; i > startIndex; i--) { 15 for (size_t i = endIndex-1; i > startIndex; i--) {
15 const size_t target = rand() % (i - startIndex) + startIndex; 16 const size_t target = Random::RandInt(startIndex, i);
16 if (i != target) { 17 if (i != target) {
17 // std::cout << "Swapping panels " << std::hex << panels[i] << " and " << std::hex << panels[target] << std::endl; 18 // std::cout << "Swapping panels " << std::hex << panels[i] << " and " << std::hex << panels[target] << std::endl;
18 SwapPanels(panels[i], panels[target], flags); 19 SwapPanels(panels[i], panels[target], flags);
diff --git a/Source/Source.vcxproj b/Source/Source.vcxproj index e46475b..afd4fd3 100644 --- a/Source/Source.vcxproj +++ b/Source/Source.vcxproj
@@ -142,7 +142,7 @@
142 <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> 142 <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
143 <ClCompile> 143 <ClCompile>
144 <PrecompiledHeader>NotUsing</PrecompiledHeader> 144 <PrecompiledHeader>NotUsing</PrecompiledHeader>
145 <WarningLevel>Level3</WarningLevel> 145 <WarningLevel>Level1</WarningLevel>
146 <Optimization>MaxSpeed</Optimization> 146 <Optimization>MaxSpeed</Optimization>
147 <FunctionLevelLinking>true</FunctionLevelLinking> 147 <FunctionLevelLinking>true</FunctionLevelLinking>
148 <IntrinsicFunctions>true</IntrinsicFunctions> 148 <IntrinsicFunctions>true</IntrinsicFunctions>
@@ -164,6 +164,7 @@
164 <ItemGroup> 164 <ItemGroup>
165 <ClInclude Include="Memory.h" /> 165 <ClInclude Include="Memory.h" />
166 <ClInclude Include="Panels.h" /> 166 <ClInclude Include="Panels.h" />
167 <ClInclude Include="Random.h" />
167 <ClInclude Include="Randomizer.h" /> 168 <ClInclude Include="Randomizer.h" />
168 <ClInclude Include="RandomizerCore.h" /> 169 <ClInclude Include="RandomizerCore.h" />
169 <ClInclude Include="Resource.h" /> 170 <ClInclude Include="Resource.h" />
diff --git a/Source/Source.vcxproj.filters b/Source/Source.vcxproj.filters index 665fc05..dbb50f9 100644 --- a/Source/Source.vcxproj.filters +++ b/Source/Source.vcxproj.filters
@@ -33,6 +33,9 @@
33 <ClInclude Include="Version.h"> 33 <ClInclude Include="Version.h">
34 <Filter>Header Files</Filter> 34 <Filter>Header Files</Filter>
35 </ClInclude> 35 </ClInclude>
36 <ClInclude Include="Random.h">
37 <Filter>Header Files</Filter>
38 </ClInclude>
36 </ItemGroup> 39 </ItemGroup>
37 <ItemGroup> 40 <ItemGroup>
38 <ClCompile Include="Main.cpp"> 41 <ClCompile Include="Main.cpp">