Lab 7-8 clean start
This commit is contained in:
parent
752e593b55
commit
9af453e459
31
2dgk_6/2dgk_6.sln
Normal file
31
2dgk_6/2dgk_6.sln
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.31729.503
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{48610DB6-1D7A-4667-B1C7-5A023FAEB4DE}") = "2dgk_6", "2dgk_6\2dgk_6.vcxproj", "{C45A2321-0319-45FB-91A2-FD1A4BB5DC4A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{C45A2321-0319-45FB-91A2-FD1A4BB5DC4A}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{C45A2321-0319-45FB-91A2-FD1A4BB5DC4A}.Debug|x64.Build.0 = Debug|x64
|
||||
{C45A2321-0319-45FB-91A2-FD1A4BB5DC4A}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{C45A2321-0319-45FB-91A2-FD1A4BB5DC4A}.Debug|x86.Build.0 = Debug|Win32
|
||||
{C45A2321-0319-45FB-91A2-FD1A4BB5DC4A}.Release|x64.ActiveCfg = Release|x64
|
||||
{C45A2321-0319-45FB-91A2-FD1A4BB5DC4A}.Release|x64.Build.0 = Release|x64
|
||||
{C45A2321-0319-45FB-91A2-FD1A4BB5DC4A}.Release|x86.ActiveCfg = Release|Win32
|
||||
{C45A2321-0319-45FB-91A2-FD1A4BB5DC4A}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {958FDA17-FFCC-4AA3-932E-C8CF2B78EBD3}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
179
2dgk_6/2dgk_6/2dgk_6.vcxproj
Normal file
179
2dgk_6/2dgk_6/2dgk_6.vcxproj
Normal file
@ -0,0 +1,179 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{c45a2321-0319-45fb-91a2-fd1a4bb5dc4a}</ProjectGuid>
|
||||
<RootNamespace>My2dgk6</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Vcpkg">
|
||||
<VcpkgEnableManifest>true</VcpkgEnableManifest>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>vcpkg_installed\x86-windows\x86-windows\include\SDL2</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>vcpkg_installed\x86-windows\x86-windows\debug\lib\manual-link;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>SDL2maind.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>vcpkg_installed\x86-windows\x86-windows\include\SDL2</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>vcpkg_installed\x86-windows\x86-windows\lib\manual-link;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>SDL2main.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>vcpkg_installed\x64-windows\x64-windows\include\SDL2</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>vcpkg_installed\x64-windows\x64-windows\debug\lib\manual-link;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>SDL2maind.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>vcpkg_installed\x64-windows\x64-windows\include\SDL2</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>vcpkg_installed\x64-windows\x64-windows\lib\manual-link;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>SDL2main.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="KGame.cpp" />
|
||||
<ClCompile Include="KInput.cpp" />
|
||||
<ClCompile Include="KTexture.cpp" />
|
||||
<ClCompile Include="KTile.cpp" />
|
||||
<ClCompile Include="Main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="vcpkg.json" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Constants.h" />
|
||||
<ClInclude Include="GamePad.h" />
|
||||
<ClInclude Include="KGame.h" />
|
||||
<ClInclude Include="KInput.h" />
|
||||
<ClInclude Include="KTexture.h" />
|
||||
<ClInclude Include="KTile.h" />
|
||||
<ClInclude Include="Utils.h" />
|
||||
<ClInclude Include="KVector2d.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
63
2dgk_6/2dgk_6/2dgk_6.vcxproj.filters
Normal file
63
2dgk_6/2dgk_6/2dgk_6.vcxproj.filters
Normal file
@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="KTile.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="KTexture.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="KGame.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="KInput.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="vcpkg.json" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Utils.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Constants.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="KVector2d.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="KTile.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="KTexture.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="KGame.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="GamePad.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="KInput.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
17
2dgk_6/2dgk_6/Constants.h
Normal file
17
2dgk_6/2dgk_6/Constants.h
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
namespace KapitanGame {
|
||||
namespace Constants {
|
||||
constexpr const char* WINDOW_TITLE = "2DGK - Zadanie 5";
|
||||
//Analog joystick dead zone
|
||||
constexpr int JOYSTICK_DEAD_ZONE = 8000;
|
||||
//Screen dimension constants
|
||||
constexpr int WINDOW_DEAD_ZONE = 100;
|
||||
constexpr int SCREEN_WIDTH = 640;
|
||||
constexpr int SCREEN_HEIGHT = 480;
|
||||
constexpr float SCREEN_RATIO = static_cast<float>(SCREEN_WIDTH) / SCREEN_HEIGHT;
|
||||
constexpr int TILE_WIDTH = 32;
|
||||
constexpr int TILE_HEIGHT = 32;
|
||||
constexpr float SPEED = 0.16f;
|
||||
constexpr float SMOOTH = 0.4f;
|
||||
}
|
||||
}
|
8
2dgk_6/2dgk_6/GamePad.h
Normal file
8
2dgk_6/2dgk_6/GamePad.h
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include <SDL_gamecontroller.h>
|
||||
namespace KapitanGame {
|
||||
struct GamePad {
|
||||
bool Buttons[SDL_CONTROLLER_BUTTON_MAX];
|
||||
int Axis[SDL_CONTROLLER_AXIS_MAX];
|
||||
};
|
||||
}
|
359
2dgk_6/2dgk_6/KGame.cpp
Normal file
359
2dgk_6/2dgk_6/KGame.cpp
Normal file
@ -0,0 +1,359 @@
|
||||
#include "KGame.h"
|
||||
#include "SDL.h"
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#include "Utils.h"
|
||||
#include "Constants.h"
|
||||
#include "KTexture.h"
|
||||
#include "KTile.h"
|
||||
#include "KVector2d.h"
|
||||
|
||||
namespace KapitanGame {
|
||||
KGame::KGame() {
|
||||
//Initialize SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
{
|
||||
throw std::runtime_error(Utils::StringFormat("SDL could not initialize! SDL_Error: %s", SDL_GetError()));
|
||||
}
|
||||
//Set texture filtering to linear
|
||||
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
|
||||
{
|
||||
printf("Warning: Linear texture filtering not enabled!\n");
|
||||
}
|
||||
|
||||
Input.Init();
|
||||
|
||||
//Create window
|
||||
Window = SDL_CreateWindow(Constants::WINDOW_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Constants::SCREEN_WIDTH, Constants::SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
if (Window == nullptr)
|
||||
{
|
||||
throw std::runtime_error(Utils::StringFormat("Window could not be created! SDL_Error: %s", SDL_GetError()));
|
||||
}
|
||||
//Create renderer for window
|
||||
Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (Renderer == nullptr)
|
||||
{
|
||||
throw std::runtime_error(Utils::StringFormat("Renderer could not be created! SDL Error: %s", SDL_GetError()));
|
||||
}
|
||||
//Initialize renderer color
|
||||
SDL_SetRenderDrawColor(Renderer, 0, 0xFF, 0, 0xFF);
|
||||
}
|
||||
|
||||
KGame::~KGame() {
|
||||
//Free loaded images
|
||||
for (int i = 0; i <= static_cast<int>(TileType::Dot); ++i)
|
||||
TileTextures[i].Free();
|
||||
|
||||
PlayerTexture.Free();
|
||||
PlayerTexture2.Free();
|
||||
|
||||
Input.Free();
|
||||
|
||||
//Destroy window
|
||||
SDL_DestroyRenderer(Renderer);
|
||||
SDL_DestroyWindow(Window);
|
||||
Window = nullptr;
|
||||
Renderer = nullptr;
|
||||
|
||||
//Quit SDL subsystems
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
bool KGame::LoadMedia()
|
||||
{
|
||||
//Loading success flag
|
||||
bool success = true;
|
||||
|
||||
if (!PlayerTexture.LoadFromFile("textures/player.bmp", Renderer))
|
||||
{
|
||||
printf("Failed to load player texture image!\n");
|
||||
success = false;
|
||||
}
|
||||
if (!PlayerTexture2.LoadFromFile("textures/player2.bmp", Renderer))
|
||||
{
|
||||
printf("Failed to load player texture image!\n");
|
||||
success = false;
|
||||
}
|
||||
|
||||
for (int i = static_cast<int>(TileType::Default); i <= static_cast<int>(TileType::Dot); ++i)
|
||||
{
|
||||
if (!TileTextures[i].LoadFromFile("textures/0" + std::to_string(i) + ".bmp", Renderer))
|
||||
{
|
||||
printf("Failed to load 0%d texture image!\n", i);
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
|
||||
Tiles.clear();
|
||||
std::ifstream levelFile;
|
||||
levelFile.open("level.txt");
|
||||
if (levelFile.fail())
|
||||
{
|
||||
printf("Failed to load level.txt!\n");
|
||||
success = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
int y = 0;
|
||||
std::string line;
|
||||
while (std::getline(levelFile, line)) {
|
||||
if (MapWidth < static_cast<int>(line.length() * Constants::TILE_WIDTH))
|
||||
MapWidth = static_cast<int>(line.length()) * Constants::TILE_WIDTH;
|
||||
for (auto i = 0ull; i < line.length(); ++i) {
|
||||
auto type = TileType::Default;
|
||||
switch (line[i])
|
||||
{
|
||||
case '|':
|
||||
type = TileType::VerticalWall;
|
||||
break;
|
||||
case '-':
|
||||
type = TileType::HorizontalWall;
|
||||
break;
|
||||
case '.':
|
||||
type = TileType::Dot;
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
Tiles.emplace_back(i * Constants::TILE_WIDTH, y * Constants::TILE_HEIGHT, type);
|
||||
}
|
||||
++y;
|
||||
}
|
||||
MapHeight = y * Constants::TILE_HEIGHT;
|
||||
levelFile.close();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
SDL_Texture* KGame::LoadTexture(const std::string& path) const {
|
||||
//The final optimized image
|
||||
SDL_Texture* newTexture = nullptr;
|
||||
|
||||
//Load image at specified path
|
||||
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
|
||||
if (loadedSurface == nullptr)
|
||||
{
|
||||
printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
//Create texture from surface pixels
|
||||
newTexture = SDL_CreateTextureFromSurface(Renderer, loadedSurface);
|
||||
if (newTexture == nullptr)
|
||||
{
|
||||
printf("Unable to optimize image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
|
||||
}
|
||||
|
||||
//Get rid of old loaded surface
|
||||
SDL_FreeSurface(loadedSurface);
|
||||
}
|
||||
|
||||
return newTexture;
|
||||
}
|
||||
|
||||
int KGame::Run(int argc, char* args[])
|
||||
{
|
||||
//Load media
|
||||
if (!LoadMedia())
|
||||
{
|
||||
printf("Failed to load media!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
//Main loop flag
|
||||
bool quit = false;
|
||||
|
||||
//Event handler
|
||||
SDL_Event e;
|
||||
|
||||
SDL_Rect viewport;
|
||||
viewport.x = Constants::SCREEN_WIDTH / 2;
|
||||
viewport.y = Constants::SCREEN_HEIGHT / 2;
|
||||
viewport.w = Constants::SCREEN_WIDTH;
|
||||
viewport.h = Constants::SCREEN_HEIGHT;
|
||||
|
||||
KVector2D playerOnePosition(0, 0);
|
||||
KVector2D playerOneInput(0, 0);
|
||||
KVector2D playerOneVelocity(0, 0);
|
||||
|
||||
KVector2D playerTwoPosition(0, 0);
|
||||
KVector2D playerTwoInput(0, 0);
|
||||
KVector2D playerTwoVelocity(0, 0);
|
||||
|
||||
SDL_Rect playerAreaModeGauntlet;
|
||||
playerAreaModeGauntlet.x = 0;
|
||||
playerAreaModeGauntlet.y = 0;
|
||||
playerAreaModeGauntlet.w = Constants::SCREEN_WIDTH - Constants::WINDOW_DEAD_ZONE;
|
||||
playerAreaModeGauntlet.h = Constants::SCREEN_HEIGHT - Constants::WINDOW_DEAD_ZONE;
|
||||
|
||||
|
||||
SDL_Rect playerAreaModeZoom;
|
||||
playerAreaModeZoom.x = 0;
|
||||
playerAreaModeZoom.y = 0;
|
||||
playerAreaModeZoom.w = (MapWidth <= MapHeight ? MapWidth : static_cast<int>(static_cast<float>(MapHeight) * Constants::SCREEN_RATIO)) - Constants::WINDOW_DEAD_ZONE;
|
||||
playerAreaModeZoom.h = (MapHeight <= MapWidth ? MapHeight : static_cast<int>(static_cast<float>(MapWidth) / Constants::SCREEN_RATIO)) - Constants::WINDOW_DEAD_ZONE;
|
||||
|
||||
float scale = 1.f;
|
||||
|
||||
//Normalized direction
|
||||
int xDir = 0;
|
||||
int yDir = 0;
|
||||
|
||||
uint32_t time, previousTime;
|
||||
time = previousTime = SDL_GetTicks();
|
||||
printf("\n");
|
||||
bool zoom = false;
|
||||
|
||||
//While application is running
|
||||
while (!quit)
|
||||
{
|
||||
previousTime = time;
|
||||
time = SDL_GetTicks();
|
||||
playerOneInput.X = 0;
|
||||
playerOneInput.Y = 0;
|
||||
|
||||
Input.HandleInputPreEvents();
|
||||
|
||||
//Handle events on queue
|
||||
while (SDL_PollEvent(&e))
|
||||
{
|
||||
//User requests quit
|
||||
if (e.type == SDL_QUIT)
|
||||
{
|
||||
quit = true;
|
||||
continue;
|
||||
}
|
||||
Input.HandleEvent(e);
|
||||
}
|
||||
Input.HandleInputPostEvents();
|
||||
|
||||
playerTwoInput.X = Input.GetControllerAxis(Controllers::Controller1, SDL_CONTROLLER_AXIS_LEFTX);
|
||||
playerTwoInput.Y = Input.GetControllerAxis(Controllers::Controller1, SDL_CONTROLLER_AXIS_LEFTY);
|
||||
|
||||
if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_W))
|
||||
{
|
||||
playerOneInput.Y = -1;
|
||||
}
|
||||
if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_S))
|
||||
{
|
||||
playerOneInput.Y = 1;
|
||||
}
|
||||
if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_A))
|
||||
{
|
||||
playerOneInput.X = -1;
|
||||
}
|
||||
if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_D))
|
||||
{
|
||||
playerOneInput.X = 1;
|
||||
}
|
||||
|
||||
playerOneInput.Normalize();
|
||||
playerOneVelocity = playerOneInput * Constants::SPEED * (1 - Constants::SMOOTH) + playerOneVelocity * Constants::SMOOTH;
|
||||
|
||||
playerOnePosition += playerOneVelocity;
|
||||
|
||||
if (Input.IsKeyboardButtonPressed(SDL_SCANCODE_F))
|
||||
{
|
||||
zoom = !zoom;
|
||||
}
|
||||
|
||||
const auto& playerArea = zoom ? playerAreaModeZoom : playerAreaModeGauntlet;
|
||||
|
||||
|
||||
// keep player one near player two
|
||||
if (playerOnePosition.X < playerTwoPosition.X - playerArea.w) playerOnePosition.X = playerTwoPosition.X - playerArea.w;
|
||||
if (playerOnePosition.X > playerTwoPosition.X + playerArea.w) playerOnePosition.X = playerTwoPosition.X + playerArea.w;
|
||||
if (playerOnePosition.Y < playerTwoPosition.Y - playerArea.h) playerOnePosition.Y = playerTwoPosition.Y - playerArea.h;
|
||||
if (playerOnePosition.Y > playerTwoPosition.Y + playerArea.h) playerOnePosition.Y = playerTwoPosition.Y + playerArea.h;
|
||||
|
||||
// keep player one in map
|
||||
if (playerOnePosition.X < 0) playerOnePosition.X = 0;
|
||||
if (playerOnePosition.X > MapWidth - PlayerTexture.GetWidth()) playerOnePosition.X = (MapWidth - PlayerTexture.GetWidth());
|
||||
if (playerOnePosition.Y < 0) playerOnePosition.Y = (0);
|
||||
if (playerOnePosition.Y > MapHeight - PlayerTexture.GetHeight()) playerOnePosition.Y = (MapHeight - PlayerTexture.GetHeight());
|
||||
|
||||
//playerTwoInput.Normalize();
|
||||
//printf("\r \rX:%f, Y:%f", playerTwoInput.x, playerTwoInput.y);
|
||||
playerTwoVelocity = playerTwoInput * Constants::SPEED * (1 - Constants::SMOOTH) + playerTwoVelocity * Constants::SMOOTH;
|
||||
|
||||
playerTwoPosition += playerTwoVelocity;
|
||||
|
||||
|
||||
if (playerTwoPosition.X < playerOnePosition.X - static_cast<float>(playerArea.w)) playerTwoPosition.X = playerOnePosition.X - playerArea.w;
|
||||
if (playerTwoPosition.X > playerOnePosition.X + playerArea.w) playerTwoPosition.X = playerOnePosition.X + playerArea.w;
|
||||
if (playerTwoPosition.Y < playerOnePosition.Y - playerArea.h) playerTwoPosition.Y = playerOnePosition.Y - playerArea.h;
|
||||
if (playerTwoPosition.Y > playerOnePosition.Y + playerArea.h) playerTwoPosition.Y = playerOnePosition.Y + playerArea.h;
|
||||
|
||||
if (playerTwoPosition.X < 0) playerTwoPosition.X = 0;
|
||||
if (playerTwoPosition.X > MapWidth - PlayerTexture.GetWidth()) playerTwoPosition.X = MapWidth - PlayerTexture.GetWidth();
|
||||
if (playerTwoPosition.Y < 0) playerTwoPosition.Y = 0;
|
||||
if (playerTwoPosition.Y > MapHeight - PlayerTexture.GetHeight()) playerTwoPosition.Y = MapHeight - PlayerTexture.GetHeight();
|
||||
|
||||
|
||||
scale = 1.f;
|
||||
if (zoom)
|
||||
{
|
||||
float xDist = std::abs(playerTwoPosition.X - playerOnePosition.X) - playerAreaModeGauntlet.w;
|
||||
float yDist = std::abs(playerTwoPosition.Y - playerOnePosition.Y) - playerAreaModeGauntlet.h;
|
||||
float xMaxDist = playerAreaModeZoom.w - playerAreaModeGauntlet.w;
|
||||
float yMaxDist = playerAreaModeZoom.h - playerAreaModeGauntlet.h;
|
||||
float t1 = Utils::Clamp(xDist / xMaxDist);
|
||||
float t2 = Utils::Clamp(yDist / yMaxDist);
|
||||
float t = std::max(std::max(t1, 0.f), std::max(t2, 0.f));
|
||||
scale = Utils::Lerp(scale, std::max(Constants::SCREEN_WIDTH * 1.0f / MapWidth, Constants::SCREEN_HEIGHT * 1.0f / MapHeight), t);
|
||||
//printf("\rscale: %f, t1: %f, t2: %f, t: %f", scale, t1, t2, t);
|
||||
}
|
||||
|
||||
KVector2D focusPoint = (playerOnePosition + playerTwoPosition + KVector2D(PlayerTexture.GetWidth(), PlayerTexture.GetHeight())+KVector2D(Constants::WINDOW_DEAD_ZONE, Constants::WINDOW_DEAD_ZONE)/2)*scale/2;
|
||||
if (Input.IsKeyboardButtonHeld(SDL_SCANCODE_Q))
|
||||
{
|
||||
focusPoint = KVector2D(MapWidth / 2.f, MapHeight / 2.f);
|
||||
}
|
||||
|
||||
//viewport.x += clamp((time - previous_time) * .05f) * (focusPoint.x - SCREEN_WIDTH / 2 - viewport.x);
|
||||
viewport.x = Utils::Lerp(viewport.x, static_cast<int>(focusPoint.X - Constants::SCREEN_WIDTH / 2.f), static_cast<float>(time - previousTime) * .05f);
|
||||
if ((focusPoint.X - Constants::SCREEN_WIDTH / 2.f) - static_cast<float>(viewport.x) <= 0.005f)
|
||||
{
|
||||
viewport.x = static_cast<int>(focusPoint.X - Constants::SCREEN_WIDTH / 2.f)/scale;
|
||||
}
|
||||
viewport.y = Utils::Lerp(viewport.y, static_cast<int>(focusPoint.Y - Constants::SCREEN_HEIGHT / 2.f), static_cast<float>(time - previousTime) * .05f);
|
||||
if (focusPoint.Y - static_cast<float>(Constants::SCREEN_HEIGHT) / 2.0f - static_cast<float>(viewport.y) <= 0.005f)
|
||||
{
|
||||
viewport.y = static_cast<int>(focusPoint.Y - Constants::SCREEN_HEIGHT / 2.f) / scale;
|
||||
}
|
||||
if (viewport.x < 0)
|
||||
viewport.x = 0;
|
||||
if (viewport.y < 0)
|
||||
viewport.y = 0;
|
||||
if (viewport.x > MapWidth - static_cast<int>(static_cast<float>(viewport.w) / scale))
|
||||
viewport.x = MapWidth - static_cast<int>(static_cast<float>(viewport.w) / scale);
|
||||
if (viewport.y > MapHeight - static_cast<int>(static_cast<float>(viewport.h) / scale))
|
||||
viewport.y = MapHeight - static_cast<int>(static_cast<float>(viewport.h) / scale);
|
||||
|
||||
//printf("\rp1(%f, %f), p2(%f, %f), viewport:(%d, %d, %d, %d), map(%d, %d), focus(%f, %f)", playerOnePosition.X * scale, playerOnePosition.Y * scale, playerTwoPosition.X * scale, playerTwoPosition.Y * scale, static_cast<int>(viewport.x * scale), static_cast<int>(viewport.y * scale), static_cast<int>(static_cast<float>(viewport.w) / scale), static_cast<int>(static_cast<float>(viewport.h) / scale), MapWidth, MapHeight, focusPoint.X, focusPoint.Y);
|
||||
|
||||
|
||||
//Clear screen
|
||||
SDL_SetRenderDrawColor(Renderer, 0, 0xFF, 0, 0xFF);
|
||||
SDL_RenderClear(Renderer);
|
||||
|
||||
|
||||
|
||||
for (auto& tile : Tiles)
|
||||
tile.Render(Renderer, TileTextures, viewport, scale);
|
||||
|
||||
PlayerTexture.Render(Renderer, playerOnePosition.X - viewport.x, playerOnePosition.Y - viewport.y, nullptr, scale);
|
||||
PlayerTexture2.Render(Renderer, playerTwoPosition.X - viewport.x, playerTwoPosition.Y - viewport.y, nullptr, scale);
|
||||
|
||||
//Update screen
|
||||
SDL_RenderPresent(Renderer);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
42
2dgk_6/2dgk_6/KGame.h
Normal file
42
2dgk_6/2dgk_6/KGame.h
Normal file
@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include "KTexture.h"
|
||||
#include <vector>
|
||||
#include <SDL_joystick.h>
|
||||
|
||||
#include "KInput.h"
|
||||
|
||||
|
||||
namespace KapitanGame {
|
||||
class KTile;
|
||||
|
||||
class KGame
|
||||
{
|
||||
public:
|
||||
KGame();
|
||||
~KGame();
|
||||
KGame(const KGame& other) = delete;
|
||||
KGame(KGame&& other) noexcept = delete;
|
||||
KGame& operator=(const KGame& other) = delete;
|
||||
KGame& operator=(KGame&& other) noexcept = delete;
|
||||
int Run(int argc, char* args[]);
|
||||
private:
|
||||
SDL_Window* Window = nullptr;
|
||||
|
||||
SDL_Renderer* Renderer = nullptr;
|
||||
|
||||
KTexture TileTextures[4];
|
||||
KTexture PlayerTexture;
|
||||
KTexture PlayerTexture2;
|
||||
std::vector<KTile> Tiles;
|
||||
|
||||
int MapHeight = -1;
|
||||
int MapWidth = -1;
|
||||
KInput Input;
|
||||
|
||||
bool LoadMedia();
|
||||
|
||||
SDL_Texture* LoadTexture(const std::string& path) const;
|
||||
};
|
||||
}
|
||||
|
||||
|
153
2dgk_6/2dgk_6/KInput.cpp
Normal file
153
2dgk_6/2dgk_6/KInput.cpp
Normal file
@ -0,0 +1,153 @@
|
||||
#include "KInput.h"
|
||||
#include "Constants.h"
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
namespace KapitanGame {
|
||||
KInput::KInput() {
|
||||
Initialized = false;
|
||||
}
|
||||
|
||||
void KInput::Init() {
|
||||
if (Initialized) return;
|
||||
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1)
|
||||
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
||||
|
||||
const int nJoysticks = SDL_NumJoysticks();
|
||||
GamepadsCount = 0;
|
||||
|
||||
// Count how many controllers there are
|
||||
for (int i = 0; i < nJoysticks; i++)
|
||||
if (SDL_IsGameController(i))
|
||||
GamepadsCount++;
|
||||
|
||||
// If we have some controllers attached
|
||||
if (GamepadsCount > 0)
|
||||
{
|
||||
for (int i = 0; i < GamepadsCount; i++)
|
||||
{
|
||||
// Open the controller and add it to our list
|
||||
SDL_GameController* pad = SDL_GameControllerOpen(i);
|
||||
if (SDL_GameControllerGetAttached(pad) == 1)
|
||||
ConnectedControllers.push_back(pad);
|
||||
else
|
||||
printf("SDL_GetError() = %s\n", SDL_GetError());
|
||||
}
|
||||
SDL_GameControllerEventState(SDL_ENABLE);
|
||||
}
|
||||
|
||||
// Vectors are empty to begin with, this sets their size
|
||||
ControllerInputs.resize(GamepadsCount);
|
||||
LastControllerInputs.resize(GamepadsCount);
|
||||
|
||||
// Set the status of the controllers to "nothing is happening"
|
||||
for (int i = 0; i < GamepadsCount; i++) {
|
||||
for (int a = 0; a < SDL_CONTROLLER_AXIS_MAX; a++) {
|
||||
ControllerInputs[i].Axis[a] = 0;
|
||||
LastControllerInputs[i].Axis[a] = 0;
|
||||
}
|
||||
for (int b = 0; b < SDL_CONTROLLER_BUTTON_MAX; b++) {
|
||||
ControllerInputs[i].Buttons[b] = false;
|
||||
LastControllerInputs[i].Buttons[b] = false;
|
||||
}
|
||||
}
|
||||
Initialized = true;
|
||||
}
|
||||
|
||||
void KInput::Free() {
|
||||
for (const auto& pad : ConnectedControllers) {
|
||||
SDL_GameControllerClose(pad);
|
||||
}
|
||||
ConnectedControllers.clear();
|
||||
}
|
||||
|
||||
void KInput::HandleInputPreEvents() {
|
||||
for (int i = 0; i < GamepadsCount; i++) {
|
||||
for (int a = 0; a < SDL_CONTROLLER_AXIS_MAX; a++) {
|
||||
LastControllerInputs[i].Axis[a] = ControllerInputs[i].Axis[a];
|
||||
}
|
||||
for (int b = 0; b < SDL_CONTROLLER_BUTTON_MAX; b++) {
|
||||
LastControllerInputs[i].Buttons[b] = ControllerInputs[i].Buttons[b];
|
||||
}
|
||||
}
|
||||
LastKeyboardInputs = KeyboardInputs;
|
||||
}
|
||||
void KInput::HandleInputPostEvents() {
|
||||
int size = 0;
|
||||
const Uint8* currentKeyStates = SDL_GetKeyboardState(&size);
|
||||
KeyboardInputs = std::vector<Uint8>(currentKeyStates, currentKeyStates + size);
|
||||
}
|
||||
void KInput::HandleEvent(const SDL_Event& event) {
|
||||
switch (event.type) {
|
||||
// This happens when a device is added
|
||||
// A future improvement is to cope with new controllers being plugged in
|
||||
// when the game is running
|
||||
case SDL_CONTROLLERDEVICEADDED:
|
||||
printf("DEVICEADDED cdevice.which = %d\n", event.cdevice.which);
|
||||
break;
|
||||
|
||||
// If a controller button is pressed
|
||||
case SDL_CONTROLLERBUTTONDOWN:
|
||||
// Cycle through the controllers
|
||||
for (int i = 0; i < GamepadsCount; i++) {
|
||||
// Looking for the button that was pressed
|
||||
if (event.cbutton.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(ConnectedControllers[i]))) {
|
||||
// So the relevant state can be updated
|
||||
ControllerInputs[i].Buttons[event.cbutton.button] = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// Do the same for releasing a button
|
||||
case SDL_CONTROLLERBUTTONUP:
|
||||
for (int i = 0; i < GamepadsCount; i++) {
|
||||
if (event.cbutton.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(ConnectedControllers[i]))) {
|
||||
ControllerInputs[i].Buttons[event.cbutton.button] = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// And something similar for axis motion
|
||||
case SDL_CONTROLLERAXISMOTION:
|
||||
for (int i = 0; i < GamepadsCount; i++) {
|
||||
if (event.cbutton.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(ConnectedControllers[i]))) {
|
||||
ControllerInputs[i].Axis[event.caxis.axis] = event.caxis.value;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
bool KInput::IsControllerButtonPressed(const Controllers controllerId, const SDL_GameControllerButton button) {
|
||||
if (controllerId < Controllers::Controller1 || controllerId > static_cast<Controllers>(GamepadsCount - 1)) return false;
|
||||
|
||||
return ControllerInputs[static_cast<int>(controllerId)].Buttons[button] && !LastControllerInputs[static_cast<int>(controllerId)].Buttons[button];
|
||||
}
|
||||
|
||||
bool KInput::IsControllerButtonHeld(const Controllers controllerId, const SDL_GameControllerButton button) {
|
||||
if (controllerId < Controllers::Controller1 || controllerId > static_cast<Controllers>(GamepadsCount - 1)) return false;
|
||||
|
||||
return ControllerInputs[static_cast<int>(controllerId)].Buttons[button] && LastControllerInputs[static_cast<int>(controllerId)].Buttons[button];
|
||||
}
|
||||
|
||||
float KInput::GetControllerAxis(const Controllers controllerId, const SDL_GameControllerAxis axis) {
|
||||
if (controllerId <Controllers::Controller1 || controllerId > static_cast<Controllers>(GamepadsCount - 1)) return 0.0;
|
||||
|
||||
const int value = ControllerInputs[static_cast<int>(controllerId)].Axis[axis];
|
||||
if (std::abs(value) < Constants::JOYSTICK_DEAD_ZONE) return 0.0;
|
||||
|
||||
return static_cast<float>(value) / 32768.0f;
|
||||
}
|
||||
|
||||
bool KInput::IsKeyboardButtonPressed(const Uint8 scanCode) {
|
||||
if (scanCode > KeyboardInputs.size() || scanCode > LastKeyboardInputs.size())
|
||||
return false;
|
||||
return KeyboardInputs[scanCode] && KeyboardInputs[scanCode] != LastKeyboardInputs[scanCode];
|
||||
}
|
||||
|
||||
bool KInput::IsKeyboardButtonHeld(const Uint8 scanCode) {
|
||||
if (scanCode > KeyboardInputs.size())
|
||||
return false;
|
||||
return KeyboardInputs[scanCode];
|
||||
}
|
||||
}
|
||||
|
38
2dgk_6/2dgk_6/KInput.h
Normal file
38
2dgk_6/2dgk_6/KInput.h
Normal file
@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include <SDL_events.h>
|
||||
#include <vector>
|
||||
|
||||
#include "GamePad.h"
|
||||
|
||||
namespace KapitanGame {
|
||||
enum class Controllers : int {
|
||||
Controller1,
|
||||
Controller2,
|
||||
Controller3,
|
||||
Controller4
|
||||
};
|
||||
|
||||
class KInput {
|
||||
std::vector<SDL_GameController*> ConnectedControllers;
|
||||
std::vector<GamePad> ControllerInputs;
|
||||
std::vector<GamePad> LastControllerInputs;
|
||||
std::vector<Uint8> KeyboardInputs;
|
||||
std::vector<Uint8> LastKeyboardInputs;
|
||||
int GamepadsCount;
|
||||
bool Initialized;
|
||||
public:
|
||||
KInput();
|
||||
void Init();
|
||||
void Free();
|
||||
void HandleInputPreEvents();
|
||||
void HandleInputPostEvents();
|
||||
void HandleEvent(const SDL_Event& event);
|
||||
bool IsControllerButtonPressed(Controllers controllerId, SDL_GameControllerButton button);
|
||||
bool IsControllerButtonHeld(Controllers controllerId, SDL_GameControllerButton button);
|
||||
float GetControllerAxis(Controllers controllerId, SDL_GameControllerAxis axis);
|
||||
bool IsKeyboardButtonHeld(Uint8 scanCode);
|
||||
bool IsKeyboardButtonPressed(Uint8 scanCode);
|
||||
};
|
||||
}
|
||||
|
||||
|
121
2dgk_6/2dgk_6/KTexture.cpp
Normal file
121
2dgk_6/2dgk_6/KTexture.cpp
Normal file
@ -0,0 +1,121 @@
|
||||
#include "KTexture.h"
|
||||
|
||||
namespace KapitanGame {
|
||||
KTexture::KTexture()
|
||||
{
|
||||
//Initialize
|
||||
Texture = nullptr;
|
||||
Width = 0;
|
||||
Height = 0;
|
||||
}
|
||||
|
||||
KTexture::KTexture(KTexture&& other) noexcept
|
||||
: Texture(other.Texture),
|
||||
Width(other.Width),
|
||||
Height(other.Height) {
|
||||
}
|
||||
|
||||
KTexture& KTexture::operator=(const KTexture& other) {
|
||||
if (this == &other)
|
||||
return *this;
|
||||
Texture = other.Texture;
|
||||
Width = other.Width;
|
||||
Height = other.Height;
|
||||
return *this;
|
||||
}
|
||||
|
||||
KTexture& KTexture::operator=(KTexture&& other) noexcept {
|
||||
if (this == &other)
|
||||
return *this;
|
||||
Texture = other.Texture;
|
||||
Width = other.Width;
|
||||
Height = other.Height;
|
||||
return *this;
|
||||
}
|
||||
|
||||
KTexture::~KTexture()
|
||||
{
|
||||
//Deallocate
|
||||
Free();
|
||||
|
||||
}
|
||||
bool KTexture::LoadFromFile(const std::string& path, SDL_Renderer* renderer)
|
||||
{
|
||||
//Get rid of preexisting texture
|
||||
Free();
|
||||
//The final texture
|
||||
SDL_Texture* newTexture = nullptr;
|
||||
|
||||
//Load image at specified path
|
||||
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
|
||||
if (loadedSurface == nullptr)
|
||||
{
|
||||
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
//Color key image
|
||||
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0xFF, 0, 0));
|
||||
//Create texture from surface pixels
|
||||
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
|
||||
if (newTexture == nullptr)
|
||||
{
|
||||
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
//Get image dimensions
|
||||
Width = loadedSurface->w;
|
||||
Height = loadedSurface->h;
|
||||
}
|
||||
|
||||
//Get rid of old loaded surface
|
||||
SDL_FreeSurface(loadedSurface);
|
||||
}
|
||||
|
||||
//Return success
|
||||
Texture = newTexture;
|
||||
return Texture != nullptr;
|
||||
}
|
||||
|
||||
void KTexture::Free()
|
||||
{
|
||||
//Free texture if it exists
|
||||
if (Texture != nullptr)
|
||||
{
|
||||
SDL_DestroyTexture(Texture);
|
||||
Texture = nullptr;
|
||||
Width = 0;
|
||||
Height = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void KTexture::Render(SDL_Renderer* renderer, const int x, const int y, SDL_Rect* clip, const float scale) const
|
||||
{
|
||||
//Set rendering space and render to screen
|
||||
SDL_Rect renderQuad = {
|
||||
static_cast<int>(static_cast<float>(x) * scale), static_cast<int>(static_cast<float>(y) * scale),
|
||||
static_cast<int>(static_cast<float>(Width) * scale), static_cast<int>(static_cast<float>(Height) * scale)
|
||||
};
|
||||
|
||||
//Set clip rendering dimensions
|
||||
if (clip != nullptr)
|
||||
{
|
||||
renderQuad.w = clip->w;
|
||||
renderQuad.h = clip->h;
|
||||
}
|
||||
|
||||
//Render to screen
|
||||
SDL_RenderCopy(renderer, Texture, clip, &renderQuad);
|
||||
}
|
||||
|
||||
int KTexture::GetWidth() const
|
||||
{
|
||||
return Width;
|
||||
}
|
||||
|
||||
int KTexture::GetHeight() const
|
||||
{
|
||||
return Height;
|
||||
}
|
||||
}
|
43
2dgk_6/2dgk_6/KTexture.h
Normal file
43
2dgk_6/2dgk_6/KTexture.h
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#include <SDL_render.h>
|
||||
#include <string>
|
||||
|
||||
namespace KapitanGame {
|
||||
//Texture wrapper class
|
||||
class KTexture {
|
||||
public:
|
||||
KTexture();
|
||||
|
||||
~KTexture();
|
||||
|
||||
KTexture(const KTexture& other) = default;
|
||||
|
||||
KTexture(KTexture&& other) noexcept;
|
||||
|
||||
KTexture& operator=(const KTexture& other);
|
||||
|
||||
KTexture& operator=(KTexture&& other) noexcept;
|
||||
|
||||
//Loads image at specified path
|
||||
bool LoadFromFile(const std::string& path, SDL_Renderer* renderer);
|
||||
|
||||
//Deallocates texture
|
||||
void Free();
|
||||
|
||||
//Renders texture at given point
|
||||
void Render(SDL_Renderer* renderer, int x, int y, SDL_Rect* clip = nullptr, float scale = 1.f) const;
|
||||
|
||||
//Gets image dimensions
|
||||
int GetWidth() const;
|
||||
int GetHeight() const;
|
||||
|
||||
private:
|
||||
//The actual hardware texture
|
||||
SDL_Texture* Texture;
|
||||
|
||||
//Image dimensions
|
||||
int Width;
|
||||
int Height;
|
||||
};
|
||||
|
||||
}
|
33
2dgk_6/2dgk_6/KTile.cpp
Normal file
33
2dgk_6/2dgk_6/KTile.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "KTile.h"
|
||||
|
||||
#include <SDL_render.h>
|
||||
|
||||
#include "Constants.h"
|
||||
#include "KTexture.h"
|
||||
|
||||
namespace KapitanGame {
|
||||
|
||||
int KTile::GetType() const {
|
||||
return static_cast<int>(Type);
|
||||
}
|
||||
|
||||
SDL_Rect KTile::GetBox() const {
|
||||
return Box;
|
||||
}
|
||||
|
||||
KTile::KTile(const int x, const int y, const TileType tileType) {
|
||||
//Get the offsets
|
||||
Box.x = x;
|
||||
Box.y = y;
|
||||
|
||||
Box.w = Constants::TILE_WIDTH;
|
||||
Box.h = Constants::TILE_HEIGHT;
|
||||
|
||||
//Get the tile type
|
||||
Type = tileType;
|
||||
}
|
||||
|
||||
void KTile::Render(SDL_Renderer* renderer, KTexture tileTextures[], const SDL_Rect& camera, const float scale) const {
|
||||
tileTextures[GetType()].Render(renderer, Box.x - camera.x, Box.y - camera.y, nullptr, scale);
|
||||
}
|
||||
}
|
36
2dgk_6/2dgk_6/KTile.h
Normal file
36
2dgk_6/2dgk_6/KTile.h
Normal file
@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include <SDL_rect.h>
|
||||
#include <cstdint>
|
||||
#include <SDL_render.h>
|
||||
|
||||
namespace KapitanGame
|
||||
{
|
||||
class KTexture;
|
||||
|
||||
enum class TileType : std::uint32_t {
|
||||
Default = 0,
|
||||
HorizontalWall = 1,
|
||||
VerticalWall = 2,
|
||||
Dot = 3
|
||||
};
|
||||
class KTile {
|
||||
public:
|
||||
//Initializes position and type
|
||||
KTile(int x, int y, TileType tileType);
|
||||
|
||||
//Shows the tile
|
||||
void Render(SDL_Renderer* renderer, KTexture tileTextures[], const SDL_Rect& camera, float scale = 1.f) const;
|
||||
|
||||
//Get the tile type
|
||||
int GetType() const;
|
||||
|
||||
SDL_Rect GetBox() const;
|
||||
|
||||
private:
|
||||
//The attributes of the tile
|
||||
SDL_Rect Box{};
|
||||
|
||||
//The tile type
|
||||
TileType Type;
|
||||
};
|
||||
}
|
61
2dgk_6/2dgk_6/KVector2d.h
Normal file
61
2dgk_6/2dgk_6/KVector2d.h
Normal file
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
#include <cmath>
|
||||
|
||||
namespace KapitanGame {
|
||||
struct KVector2D {
|
||||
float X;
|
||||
float Y;
|
||||
|
||||
KVector2D(const float x, const float y) : X(x), Y(y) {}
|
||||
|
||||
float Length() const { return sqrt(X * X + Y * Y); }
|
||||
|
||||
KVector2D operator+(const KVector2D& v2) const {
|
||||
return { X + v2.X, Y + v2.Y };
|
||||
}
|
||||
|
||||
friend KVector2D& operator+=(KVector2D& v1, const KVector2D& v2) {
|
||||
v1.X += v2.X;
|
||||
v1.Y += v2.Y;
|
||||
return v1;
|
||||
}
|
||||
|
||||
KVector2D operator*(const float scalar) const
|
||||
{
|
||||
return { X * scalar, Y * scalar };
|
||||
}
|
||||
|
||||
KVector2D& operator*=(const float scalar) {
|
||||
X *= scalar;
|
||||
Y *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
KVector2D operator-(const KVector2D& v2) const {
|
||||
return { X - v2.X, Y - v2.Y };
|
||||
}
|
||||
friend KVector2D& operator-=(KVector2D& v1, const KVector2D& v2) {
|
||||
v1.X -= v2.X;
|
||||
v1.Y -= v2.Y;
|
||||
return v1;
|
||||
}
|
||||
|
||||
KVector2D operator/(const float scalar) const
|
||||
{
|
||||
return { X / scalar, Y / scalar };
|
||||
}
|
||||
KVector2D& operator/=(const float scalar) {
|
||||
X /= scalar;
|
||||
Y /= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Normalize() {
|
||||
const float l = Length();
|
||||
if (l > 0) {
|
||||
(*this) *= 1 / l;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
8
2dgk_6/2dgk_6/Main.cpp
Normal file
8
2dgk_6/2dgk_6/Main.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "KGame.h"
|
||||
|
||||
int main(int argc, char* args[])
|
||||
{
|
||||
KapitanGame::KGame game;
|
||||
|
||||
return game.Run(argc, args);
|
||||
}
|
30
2dgk_6/2dgk_6/Utils.h
Normal file
30
2dgk_6/2dgk_6/Utils.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
namespace KapitanGame {
|
||||
namespace Utils {
|
||||
|
||||
template<typename ... Args>
|
||||
std::string StringFormat(const std::string& format, Args ... args)
|
||||
{
|
||||
const int sizeS = std::snprintf(nullptr, 0, format.c_str(), args ...) + 1; // Extra space for '\0'
|
||||
if (sizeS <= 0) { throw std::runtime_error("Error during formatting."); }
|
||||
const auto size = static_cast<size_t>(sizeS);
|
||||
const auto buf = std::make_unique<char[]>(size);
|
||||
std::snprintf(buf.get(), size, format.c_str(), args ...);
|
||||
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
|
||||
}
|
||||
template <class T>
|
||||
constexpr const T& Clamp(const T& x)
|
||||
{
|
||||
return std::max(std::min(1.f, x), 0.f);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
constexpr const T& Lerp(const T& a, const T& b, const float& t)
|
||||
{
|
||||
return a + (b - a) * t;
|
||||
}
|
||||
}
|
||||
}
|
31
2dgk_6/2dgk_6/level.txt
Normal file
31
2dgk_6/2dgk_6/level.txt
Normal file
@ -0,0 +1,31 @@
|
||||
/------------\/------------\
|
||||
|............||............|
|
||||
|./--\./---\.||./---\./--\.|
|
||||
|#| |.| |.||.| |.| |#|
|
||||
|.`--'.`---'.`'.`---'.`--'.|
|
||||
|..........................|
|
||||
|./--\./\./------\./\./--\.|
|
||||
|.`--'.||.`--\/--'.||.`--'.|
|
||||
|......||....||....||......|
|
||||
`----\.|`--\ || /--'|./----'
|
||||
|.|/--' `' `--\|.|
|
||||
|.|| ||.|
|
||||
|.|| /--==--\ ||.|
|
||||
-----'.`' | | `'.`-----
|
||||
. | | .
|
||||
-----\./\ | | /\./-----
|
||||
|.|| `------' ||.|
|
||||
|.|| ||.|
|
||||
|.|| /------\ ||.|
|
||||
/----'.`' `--\/--' `'.`----\
|
||||
|............||............|
|
||||
|./--\./---\.||./---\./--\.|
|
||||
|.`-\|.`---'.`'.`---'.|/-'.|
|
||||
|#..||....... .......||..#|
|
||||
`-\.||./\./------\./\.||./-'
|
||||
/-'.`'.||.`--\/--'.||.`'.`-\
|
||||
|......||....||....||......|
|
||||
|./----'`--\.||./--'`----\.|
|
||||
|.`--------'.`'.`--------'.|
|
||||
|..........................|
|
||||
`--------------------------'
|
BIN
2dgk_6/2dgk_6/textures/00.bmp
(Stored with Git LFS)
Normal file
BIN
2dgk_6/2dgk_6/textures/00.bmp
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
2dgk_6/2dgk_6/textures/01.bmp
(Stored with Git LFS)
Normal file
BIN
2dgk_6/2dgk_6/textures/01.bmp
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
2dgk_6/2dgk_6/textures/02.bmp
(Stored with Git LFS)
Normal file
BIN
2dgk_6/2dgk_6/textures/02.bmp
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
2dgk_6/2dgk_6/textures/03.bmp
(Stored with Git LFS)
Normal file
BIN
2dgk_6/2dgk_6/textures/03.bmp
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
2dgk_6/2dgk_6/textures/player.bmp
(Stored with Git LFS)
Normal file
BIN
2dgk_6/2dgk_6/textures/player.bmp
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
2dgk_6/2dgk_6/textures/player2.bmp
(Stored with Git LFS)
Normal file
BIN
2dgk_6/2dgk_6/textures/player2.bmp
(Stored with Git LFS)
Normal file
Binary file not shown.
8
2dgk_6/2dgk_6/vcpkg.json
Normal file
8
2dgk_6/2dgk_6/vcpkg.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
|
||||
"name": "dgk-6",
|
||||
"version": "0.1.0",
|
||||
"dependencies": [
|
||||
"sdl2"
|
||||
]
|
||||
}
|
7
2dgk_6/readme.txt
Normal file
7
2dgk_6/readme.txt
Normal file
@ -0,0 +1,7 @@
|
||||
IDE: Microsoft Visual Studio 2019
|
||||
Kompilator: Microsoft Visual C++
|
||||
Biblioteki: SDL2
|
||||
|
||||
Przetestowane pady: DualSense, DualShock 3 (ze sterownikiem/wrapperem XInputowym ScpToolkit), telefon Android (ze sterownikiem/wrapperem XInputowym HandyGamePad).
|
||||
|
||||
Uwaga: Do pobrania bibliotek projekt wykorzystuje vcpkg, menedżer bibliotek C++, w trybie Manifest. Należy go pobrać i skonfigurować zgodnie z instrukcją: https://github.com/microsoft/vcpkg#getting-started
|
Loading…
Reference in New Issue
Block a user