From 755d6ed73e9503d70662574cf8ea4baccd657d1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Le=C5=9Bniak?= Date: Sat, 30 Oct 2021 19:36:36 +0200 Subject: [PATCH] Zadania 1-4 --- .gitattributes | 1 + .gitignore | 401 ++++++++++++++++++++ 2dgk_1/2dgk_1.sln | 31 ++ 2dgk_1/2dgk_1/2dgk_1.vcxproj | 159 ++++++++ 2dgk_1/2dgk_1/2dgk_1.vcxproj.filters | 22 ++ 2dgk_1/2dgk_1/main.cpp | 351 +++++++++++++++++ 2dgk_1/2dgk_1/textures/01.bmp | 3 + 2dgk_1/2dgk_1/textures/02.bmp | 3 + 2dgk_1/2dgk_1/textures/sprite.bmp | 3 + 2dgk_2/2dgk_2.sln | 31 ++ 2dgk_2/2dgk_2/2dgk_2.vcxproj | 169 +++++++++ 2dgk_2/2dgk_2/2dgk_2.vcxproj.filters | 25 ++ 2dgk_2/2dgk_2/main.cpp | 425 +++++++++++++++++++++ 2dgk_2/2dgk_2/textures/01.bmp | 3 + 2dgk_2/2dgk_2/textures/02.bmp | 3 + 2dgk_2/2dgk_2/vcpkg.json | 8 + 2dgk_3/2dgk_3.sln | 31 ++ 2dgk_3/2dgk_3/2dgk_3.vcxproj | 164 ++++++++ 2dgk_3/2dgk_3/2dgk_3.vcxproj.filters | 25 ++ 2dgk_3/2dgk_3/level.txt | 31 ++ 2dgk_3/2dgk_3/main.cpp | 464 +++++++++++++++++++++++ 2dgk_3/2dgk_3/textures/00.bmp | 3 + 2dgk_3/2dgk_3/textures/01.bmp | 3 + 2dgk_3/2dgk_3/textures/02.bmp | 3 + 2dgk_3/2dgk_3/textures/03.bmp | 3 + 2dgk_3/2dgk_3/vcpkg.json | 8 + 2dgk_4/2dgk_4.sln | 31 ++ 2dgk_4/2dgk_4/2dgk_4.vcxproj | 165 ++++++++ 2dgk_4/2dgk_4/2dgk_4.vcxproj.filters | 25 ++ 2dgk_4/2dgk_4/level.txt | 31 ++ 2dgk_4/2dgk_4/main.cpp | 537 +++++++++++++++++++++++++++ 2dgk_4/2dgk_4/textures/00.bmp | 3 + 2dgk_4/2dgk_4/textures/01.bmp | 3 + 2dgk_4/2dgk_4/textures/02.bmp | 3 + 2dgk_4/2dgk_4/textures/03.bmp | 3 + 2dgk_4/2dgk_4/textures/player.bmp | 3 + 2dgk_4/2dgk_4/vcpkg.json | 8 + 37 files changed, 3185 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 2dgk_1/2dgk_1.sln create mode 100644 2dgk_1/2dgk_1/2dgk_1.vcxproj create mode 100644 2dgk_1/2dgk_1/2dgk_1.vcxproj.filters create mode 100644 2dgk_1/2dgk_1/main.cpp create mode 100644 2dgk_1/2dgk_1/textures/01.bmp create mode 100644 2dgk_1/2dgk_1/textures/02.bmp create mode 100644 2dgk_1/2dgk_1/textures/sprite.bmp create mode 100644 2dgk_2/2dgk_2.sln create mode 100644 2dgk_2/2dgk_2/2dgk_2.vcxproj create mode 100644 2dgk_2/2dgk_2/2dgk_2.vcxproj.filters create mode 100644 2dgk_2/2dgk_2/main.cpp create mode 100644 2dgk_2/2dgk_2/textures/01.bmp create mode 100644 2dgk_2/2dgk_2/textures/02.bmp create mode 100644 2dgk_2/2dgk_2/vcpkg.json create mode 100644 2dgk_3/2dgk_3.sln create mode 100644 2dgk_3/2dgk_3/2dgk_3.vcxproj create mode 100644 2dgk_3/2dgk_3/2dgk_3.vcxproj.filters create mode 100644 2dgk_3/2dgk_3/level.txt create mode 100644 2dgk_3/2dgk_3/main.cpp create mode 100644 2dgk_3/2dgk_3/textures/00.bmp create mode 100644 2dgk_3/2dgk_3/textures/01.bmp create mode 100644 2dgk_3/2dgk_3/textures/02.bmp create mode 100644 2dgk_3/2dgk_3/textures/03.bmp create mode 100644 2dgk_3/2dgk_3/vcpkg.json create mode 100644 2dgk_4/2dgk_4.sln create mode 100644 2dgk_4/2dgk_4/2dgk_4.vcxproj create mode 100644 2dgk_4/2dgk_4/2dgk_4.vcxproj.filters create mode 100644 2dgk_4/2dgk_4/level.txt create mode 100644 2dgk_4/2dgk_4/main.cpp create mode 100644 2dgk_4/2dgk_4/textures/00.bmp create mode 100644 2dgk_4/2dgk_4/textures/01.bmp create mode 100644 2dgk_4/2dgk_4/textures/02.bmp create mode 100644 2dgk_4/2dgk_4/textures/03.bmp create mode 100644 2dgk_4/2dgk_4/textures/player.bmp create mode 100644 2dgk_4/2dgk_4/vcpkg.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..95c1536 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.bmp filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..efbd48b --- /dev/null +++ b/.gitignore @@ -0,0 +1,401 @@ +# ---> VisualStudio +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ +[Vv]cpkg_installed/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# ---> C++ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +*.png +*.zip \ No newline at end of file diff --git a/2dgk_1/2dgk_1.sln b/2dgk_1/2dgk_1.sln new file mode 100644 index 0000000..6b2e846 --- /dev/null +++ b/2dgk_1/2dgk_1.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31727.386 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2dgk_1", "2dgk_1\2dgk_1.vcxproj", "{879303A0-7139-4EFA-97EB-8E6AE360EE92}" +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 + {879303A0-7139-4EFA-97EB-8E6AE360EE92}.Debug|x64.ActiveCfg = Debug|x64 + {879303A0-7139-4EFA-97EB-8E6AE360EE92}.Debug|x64.Build.0 = Debug|x64 + {879303A0-7139-4EFA-97EB-8E6AE360EE92}.Debug|x86.ActiveCfg = Debug|Win32 + {879303A0-7139-4EFA-97EB-8E6AE360EE92}.Debug|x86.Build.0 = Debug|Win32 + {879303A0-7139-4EFA-97EB-8E6AE360EE92}.Release|x64.ActiveCfg = Release|x64 + {879303A0-7139-4EFA-97EB-8E6AE360EE92}.Release|x64.Build.0 = Release|x64 + {879303A0-7139-4EFA-97EB-8E6AE360EE92}.Release|x86.ActiveCfg = Release|Win32 + {879303A0-7139-4EFA-97EB-8E6AE360EE92}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {BD596876-B119-42B8-8086-A498FEBD19EF} + EndGlobalSection +EndGlobal diff --git a/2dgk_1/2dgk_1/2dgk_1.vcxproj b/2dgk_1/2dgk_1/2dgk_1.vcxproj new file mode 100644 index 0000000..b6b78b3 --- /dev/null +++ b/2dgk_1/2dgk_1/2dgk_1.vcxproj @@ -0,0 +1,159 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + 16.0 + Win32Proj + {879303a0-7139-4efa-97eb-8e6ae360ee92} + My2dgk1 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + C:\vcpkg\installed\x86-windows\debug\lib\manual-link;$(LibraryPath) + C:\vcpkg\installed\x86-windows\include\SDL2;$(IncludePath) + + + false + C:\vcpkg\installed\x86-windows\lib\manual-link;$(LibraryPath) + C:\vcpkg\installed\x86-windows\include\SDL2;$(IncludePath) + + + true + + + false + + + ..\..\..\..\..\..\vcpkg\installed + + + ..\..\..\..\..\..\vcpkg\installed + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + SDL2maind.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + SDL2main.lib;%(AdditionalDependencies) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/2dgk_1/2dgk_1/2dgk_1.vcxproj.filters b/2dgk_1/2dgk_1/2dgk_1.vcxproj.filters new file mode 100644 index 0000000..ce0c35c --- /dev/null +++ b/2dgk_1/2dgk_1/2dgk_1.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/2dgk_1/2dgk_1/main.cpp b/2dgk_1/2dgk_1/main.cpp new file mode 100644 index 0000000..b4dd920 --- /dev/null +++ b/2dgk_1/2dgk_1/main.cpp @@ -0,0 +1,351 @@ +/*This source code copyrighted by Lazy Foo' Productions (2004-2020) +and may not be redistributed without written permission.*/ + +//Using SDL and standard IO +#include +#include +#include + +//Screen dimension constants +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +//Texture wrapper class +class KTexture +{ +public: + //Initializes variables + KTexture(); + + //Deallocates memory + ~KTexture(); + + //Loads image at specified path + bool loadFromFile(std::string path); + + //Deallocates texture + void free(); + + //Renders texture at given point + void render(int x, int y, SDL_Rect* clip = NULL); + + //Gets image dimensions + int getWidth(); + int getHeight(); + +private: + //The actual hardware texture + SDL_Texture* mTexture; + + //Image dimensions + int mWidth; + int mHeight; +}; + +//Scene sprites +SDL_Rect gSpriteClips[2]; +KTexture gSpriteSheetTexture; + +SDL_Window* gWindow = NULL; + +SDL_Renderer* gRenderer = NULL; + +KTexture gTexture1; +KTexture gTexture2; + +bool init(); + +bool loadMedia(); + +void close(); +SDL_Texture* loadTexture(std::string path); + +KTexture::KTexture() +{ + //Initialize + mTexture = NULL; + mWidth = 0; + mHeight = 0; +} + +KTexture::~KTexture() +{ + //Deallocate + free(); +} + +bool KTexture::loadFromFile(std::string path) +{ + //Get rid of preexisting texture + free(); + //The final texture + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + 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(gRenderer, loadedSurface); + if (newTexture == NULL) + { + printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Get image dimensions + mWidth = loadedSurface->w; + mHeight = loadedSurface->h; + } + + //Get rid of old loaded surface + SDL_FreeSurface(loadedSurface); + } + + //Return success + mTexture = newTexture; + return mTexture != NULL; +} + +void KTexture::free() +{ + //Free texture if it exists + if (mTexture != NULL) + { + SDL_DestroyTexture(mTexture); + mTexture = NULL; + mWidth = 0; + mHeight = 0; + } +} + +void KTexture::render(int x, int y, SDL_Rect* clip) +{ + //Set rendering space and render to screen + SDL_Rect renderQuad = { x, y, mWidth, mHeight }; + + //Set clip rendering dimensions + if (clip != NULL) + { + renderQuad.w = clip->w; + renderQuad.h = clip->h; + } + + //Render to screen + SDL_RenderCopy(gRenderer, mTexture, clip, &renderQuad); +} + +int KTexture::getWidth() +{ + return mWidth; +} + +int KTexture::getHeight() +{ + return mHeight; +} + +bool init() +{ + //Initialization flag + bool success = true; + + //Initialize SDL + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Set texture filtering to linear + if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) + { + printf("Warning: Linear texture filtering not enabled!"); + } + //Create window + gWindow = SDL_CreateWindow("2DGK - Zadanie 1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (gWindow == NULL) + { + printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Create renderer for window + gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); + if (gRenderer == NULL) + { + printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Initialize renderer color + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + } + } + } + + return success; +} + +bool loadMedia() +{ + //Loading success flag + bool success = true; + + + if (!gTexture1.loadFromFile("textures/01.bmp")) + { + printf("Failed to load 01 texture image!\n"); + success = false; + } + + if (!gTexture2.loadFromFile("textures/02.bmp")) + { + printf("Failed to load 01 texture image!\n"); + success = false; + } + + //Load sprite sheet texture + if (!gSpriteSheetTexture.loadFromFile("textures/sprite.bmp")) + { + printf("Failed to load sprite sheet texture!\n"); + success = false; + } + else + { + //Set top left sprite + gSpriteClips[0].x = 0; + gSpriteClips[0].y = 0; + gSpriteClips[0].w = 100; + gSpriteClips[0].h = 100; + + //Set top right sprite + gSpriteClips[1].x = 100; + gSpriteClips[1].y = 0; + gSpriteClips[1].w = 100; + gSpriteClips[1].h = 100; + } + + return success; +} + +SDL_Texture* loadTexture(std::string path) +{ + //The final optimized image + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Create texture from surface pixels + newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface); + if (newTexture == NULL) + { + 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; +} + +void close() +{ + //Free loaded images + gTexture1.free(); + gTexture2.free(); + gSpriteSheetTexture.free(); + + //Destroy window + SDL_DestroyRenderer(gRenderer); + SDL_DestroyWindow(gWindow); + gWindow = NULL; + gRenderer = NULL; + + //Quit SDL subsystems + SDL_Quit(); +} + +int main(int argc, char* args[]) +{ + //Start up SDL and create window + if (!init()) + { + printf("Failed to initialize!\n"); + } + else + { + //Load media + if (!loadMedia()) + { + printf("Failed to load media!\n"); + } + else + { + //Main loop flag + bool quit = false; + + //Event handler + SDL_Event e; + + int posX, posY; + posX = 0; + posY = 290; + + //While application is running + while (!quit) + { + //Handle events on queue + while (SDL_PollEvent(&e) != 0) + { + //User requests quit + if (e.type == SDL_QUIT) + { + quit = true; + } + } + + //Clear screen + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + SDL_RenderClear(gRenderer); + + gTexture1.render(0, 0); + + gTexture2.render(250, 0); + + if (SDL_GetTicks() % 10 == 0) + { + posX--; + posY--; + } + if (posX < -100) posX = SCREEN_WIDTH; + if (posY < -100) posY = SCREEN_HEIGHT; + gSpriteSheetTexture.render(posX, posY, &gSpriteClips[SDL_GetTicks() % 2]); + + //Update screen + SDL_RenderPresent(gRenderer); + } + } + } + + //Free resources and close SDL + close(); + + return 0; +} \ No newline at end of file diff --git a/2dgk_1/2dgk_1/textures/01.bmp b/2dgk_1/2dgk_1/textures/01.bmp new file mode 100644 index 0000000..2ad844e --- /dev/null +++ b/2dgk_1/2dgk_1/textures/01.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1041b9e955d07ef005e8dd7f54ca683c82c1ae2419cc69a5dcffb7cd96913dde +size 49206 diff --git a/2dgk_1/2dgk_1/textures/02.bmp b/2dgk_1/2dgk_1/textures/02.bmp new file mode 100644 index 0000000..99bd136 --- /dev/null +++ b/2dgk_1/2dgk_1/textures/02.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:65967b48c9b83e7f1d74a254f6576adeb2ef3222b09e4281a38a146c88da3217 +size 49206 diff --git a/2dgk_1/2dgk_1/textures/sprite.bmp b/2dgk_1/2dgk_1/textures/sprite.bmp new file mode 100644 index 0000000..f4cfa2f --- /dev/null +++ b/2dgk_1/2dgk_1/textures/sprite.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1895a45572c478e73bc7c3d7c7b109b01b24d6a0a52f6f76a34a29ba3ee474da +size 60054 diff --git a/2dgk_2/2dgk_2.sln b/2dgk_2/2dgk_2.sln new file mode 100644 index 0000000..85b2472 --- /dev/null +++ b/2dgk_2/2dgk_2.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31727.386 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2dgk_2", "2dgk_2\2dgk_2.vcxproj", "{688F3DFA-5E3F-414A-A983-673EB46D2711}" +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 + {688F3DFA-5E3F-414A-A983-673EB46D2711}.Debug|x64.ActiveCfg = Debug|x64 + {688F3DFA-5E3F-414A-A983-673EB46D2711}.Debug|x64.Build.0 = Debug|x64 + {688F3DFA-5E3F-414A-A983-673EB46D2711}.Debug|x86.ActiveCfg = Debug|Win32 + {688F3DFA-5E3F-414A-A983-673EB46D2711}.Debug|x86.Build.0 = Debug|Win32 + {688F3DFA-5E3F-414A-A983-673EB46D2711}.Release|x64.ActiveCfg = Release|x64 + {688F3DFA-5E3F-414A-A983-673EB46D2711}.Release|x64.Build.0 = Release|x64 + {688F3DFA-5E3F-414A-A983-673EB46D2711}.Release|x86.ActiveCfg = Release|Win32 + {688F3DFA-5E3F-414A-A983-673EB46D2711}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2ED71AB1-46CA-4D93-A1B0-1EF80C2919A1} + EndGlobalSection +EndGlobal diff --git a/2dgk_2/2dgk_2/2dgk_2.vcxproj b/2dgk_2/2dgk_2/2dgk_2.vcxproj new file mode 100644 index 0000000..30d4166 --- /dev/null +++ b/2dgk_2/2dgk_2/2dgk_2.vcxproj @@ -0,0 +1,169 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {688f3dfa-5e3f-414a-a983-673eb46d2711} + My2dgk2 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + $(LibraryPath) + $(IncludePath) + + + false + $(IncludePath) + $(LibraryPath) + + + true + + + false + + + true + + + x86-windows + + + x86-windows + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x86-windows\x86-windows\include\SDL2;%(AdditionalIncludeDirectories) + + + Console + true + vcpkg_installed\x86-windows\x86-windows\debug\lib\manual-link + SDL2maind.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x86-windows\x86-windows\include\SDL2;%(AdditionalIncludeDirectories) + + + Console + true + true + true + vcpkg_installed\x86-windows\x86-windows\lib\manual-link + SDL2main.lib;%(AdditionalDependencies) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/2dgk_2/2dgk_2/2dgk_2.vcxproj.filters b/2dgk_2/2dgk_2/2dgk_2.vcxproj.filters new file mode 100644 index 0000000..e3ec074 --- /dev/null +++ b/2dgk_2/2dgk_2/2dgk_2.vcxproj.filters @@ -0,0 +1,25 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + Source Files + + + \ No newline at end of file diff --git a/2dgk_2/2dgk_2/main.cpp b/2dgk_2/2dgk_2/main.cpp new file mode 100644 index 0000000..38ed562 --- /dev/null +++ b/2dgk_2/2dgk_2/main.cpp @@ -0,0 +1,425 @@ +//Using SDL and standard IO +#include "SDL.h" +#include +#include + +//Screen dimension constants +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; +const float SPEED = 0.16f; +const float SMOOTH = 0.5f; + +class Vector2D { +public: + + Vector2D(float x, float y) : m_x(x), m_y(y) {} + float getX() { return m_x; } + float getY() { return m_y; } + + void setX(float x) { m_x = x; } + void setY(float y) { m_y = y; } + + float length() { return sqrt(m_x * m_x + m_y * m_y); } + + Vector2D operator+(const Vector2D& v2) const { + return Vector2D(m_x + v2.m_x, m_y + v2.m_y); + } + + friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2) { + v1.m_x += v2.m_x; + v1.m_y += v2.m_y; + return v1; + } + + Vector2D operator*(float scalar) { + return Vector2D(m_x * scalar, m_y * scalar); + } + + Vector2D& operator*=(float scalar) { + m_x *= scalar; + m_y *= scalar; + return *this; + } + + Vector2D operator-(const Vector2D& v2) const { + return Vector2D(m_x - v2.m_x, m_y - v2.m_y); + } + friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2) { + v1.m_x -= v2.m_x; + v1.m_y -= v2.m_y; + return v1; + } + + Vector2D operator/(float scalar) { + return Vector2D(m_x / scalar, m_y / scalar); + } + Vector2D& operator/=(float scalar) { + m_x /= scalar; + m_y /= scalar; + return *this; + } + + void normalize() { + float l = length(); + if (l > 0) { + (*this) *= 1 / l; + } + } + +private: + + float m_x; + float m_y; +}; + +//Texture wrapper class +class KTexture +{ +public: + //Initializes variables + KTexture(); + + //Deallocates memory + ~KTexture(); + + //Loads image at specified path + bool loadFromFile(std::string path); + + //Deallocates texture + void free(); + + //Renders texture at given point + void render(int x, int y, SDL_Rect* clip = NULL); + + //Gets image dimensions + int getWidth(); + int getHeight(); + +private: + //The actual hardware texture + SDL_Texture* mTexture; + + //Image dimensions + int mWidth; + int mHeight; +}; + +SDL_Window* gWindow = NULL; + +SDL_Renderer* gRenderer = NULL; + +KTexture gTexture1; +KTexture gTexture2; + +bool init(); + +bool loadMedia(); + +void close(); +SDL_Texture* loadTexture(std::string path); + +KTexture::KTexture() +{ + //Initialize + mTexture = NULL; + mWidth = 0; + mHeight = 0; +} + +KTexture::~KTexture() +{ + //Deallocate + free(); +} + +bool KTexture::loadFromFile(std::string path) +{ + //Get rid of preexisting texture + free(); + //The final texture + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + 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(gRenderer, loadedSurface); + if (newTexture == NULL) + { + printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Get image dimensions + mWidth = loadedSurface->w; + mHeight = loadedSurface->h; + } + + //Get rid of old loaded surface + SDL_FreeSurface(loadedSurface); + } + + //Return success + mTexture = newTexture; + return mTexture != NULL; +} + +void KTexture::free() +{ + //Free texture if it exists + if (mTexture != NULL) + { + SDL_DestroyTexture(mTexture); + mTexture = NULL; + mWidth = 0; + mHeight = 0; + } +} + +void KTexture::render(int x, int y, SDL_Rect* clip) +{ + //Set rendering space and render to screen + SDL_Rect renderQuad = { x, y, mWidth, mHeight }; + + //Set clip rendering dimensions + if (clip != NULL) + { + renderQuad.w = clip->w; + renderQuad.h = clip->h; + } + + //Render to screen + SDL_RenderCopy(gRenderer, mTexture, clip, &renderQuad); +} + +int KTexture::getWidth() +{ + return mWidth; +} + +int KTexture::getHeight() +{ + return mHeight; +} + +bool init() +{ + //Initialization flag + bool success = true; + + //Initialize SDL + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Set texture filtering to linear + if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) + { + printf("Warning: Linear texture filtering not enabled!"); + } + //Create window + gWindow = SDL_CreateWindow("2DGK - Zadanie 2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (gWindow == NULL) + { + printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Create renderer for window + gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); + if (gRenderer == NULL) + { + printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Initialize renderer color + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + } + } + } + + return success; +} + +bool loadMedia() +{ + //Loading success flag + bool success = true; + + + if (!gTexture1.loadFromFile("textures/01.bmp")) + { + printf("Failed to load 01 texture image!\n"); + success = false; + } + + if (!gTexture2.loadFromFile("textures/02.bmp")) + { + printf("Failed to load 01 texture image!\n"); + success = false; + } + + return success; +} + +SDL_Texture* loadTexture(std::string path) +{ + //The final optimized image + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Create texture from surface pixels + newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface); + if (newTexture == NULL) + { + 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; +} + +void close() +{ + //Free loaded images + gTexture1.free(); + gTexture2.free(); + + //Destroy window + SDL_DestroyRenderer(gRenderer); + SDL_DestroyWindow(gWindow); + gWindow = NULL; + gRenderer = NULL; + + //Quit SDL subsystems + SDL_Quit(); +} + +int main(int argc, char* args[]) +{ + //Start up SDL and create window + if (!init()) + { + printf("Failed to initialize!\n"); + } + else + { + //Load media + if (!loadMedia()) + { + printf("Failed to load media!\n"); + } + else + { + //Main loop flag + bool quit = false; + + //Event handler + SDL_Event e; + + Vector2D playerOnePosition(0, 0); + Vector2D playerOneInput(0, 0); + Vector2D playerOneVelocity(0, 0); + Vector2D playerTwoPosition(0, 0); + Uint8 previousKeyStates[] = {0,0,0,0}; + //While application is running + while (!quit) + { + playerOneInput.setX(0); + playerOneInput.setY(0); + //Handle events on queue + while (SDL_PollEvent(&e) != 0) + { + //User requests quit + if (e.type == SDL_QUIT) + { + quit = true; + } + else if (e.type == SDL_MOUSEMOTION) + { + int x, y; + SDL_GetMouseState(&x, &y); + printf("[MOUSEMOTION]: x=%d, y=%d\n", x, y); + playerTwoPosition.setX(x - gTexture2.getWidth() / 2); + playerTwoPosition.setY(y - gTexture2.getHeight() / 2); + } + } + + const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); + if (currentKeyStates[SDL_SCANCODE_W]) + { + playerOneInput.setY(-1); + printf("[KBD] W is pressed!\n"); + } + if (currentKeyStates[SDL_SCANCODE_S]) + { + playerOneInput.setY(1); + printf("[KBD] S is pressed!\n"); + } + if (currentKeyStates[SDL_SCANCODE_A]) + { + playerOneInput.setX(-1); + printf("[KBD] A is pressed!\n"); + } + if (currentKeyStates[SDL_SCANCODE_D]) + { + playerOneInput.setX(1); + printf("[KBD] D is pressed!\n"); + } + + playerOneInput.normalize(); + playerOneVelocity = playerOneInput * SPEED * (1 - SMOOTH) + playerOneVelocity * SMOOTH; + + playerOnePosition += playerOneVelocity; + + if (playerOnePosition.getX() < 0) playerOnePosition.setX(0); + if (playerOnePosition.getX() > SCREEN_WIDTH - gTexture1.getWidth()) playerOnePosition.setX(SCREEN_WIDTH - gTexture1.getWidth()); + if (playerOnePosition.getY() < 0) playerOnePosition.setY(0); + if (playerOnePosition.getY() > SCREEN_HEIGHT - gTexture1.getHeight()) playerOnePosition.setY(SCREEN_HEIGHT - gTexture1.getHeight()); + + + //Clear screen + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + SDL_RenderClear(gRenderer); + + + gTexture2.render(playerTwoPosition.getX(), playerTwoPosition.getY()); gTexture1.render(playerOnePosition.getX(), playerOnePosition.getY()); + + + + + + //Update screen + SDL_RenderPresent(gRenderer); + } + } + } + + //Free resources and close SDL + close(); + + return 0; +} \ No newline at end of file diff --git a/2dgk_2/2dgk_2/textures/01.bmp b/2dgk_2/2dgk_2/textures/01.bmp new file mode 100644 index 0000000..b6a63fc --- /dev/null +++ b/2dgk_2/2dgk_2/textures/01.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:54014d6e82ec8dc2979369b0874e32c973a4e3cec5355d525361005e9d1f0b2f +size 12342 diff --git a/2dgk_2/2dgk_2/textures/02.bmp b/2dgk_2/2dgk_2/textures/02.bmp new file mode 100644 index 0000000..fad6efe --- /dev/null +++ b/2dgk_2/2dgk_2/textures/02.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ff0d474d0e17e0cd97944a51b5c9e96656bb406823d428c922f2b1967887cbfb +size 12342 diff --git a/2dgk_2/2dgk_2/vcpkg.json b/2dgk_2/2dgk_2/vcpkg.json new file mode 100644 index 0000000..6c4f3e7 --- /dev/null +++ b/2dgk_2/2dgk_2/vcpkg.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", + "name": "dgk-2", + "version": "0.1.0", + "dependencies": [ + "sdl2" + ] +} \ No newline at end of file diff --git a/2dgk_3/2dgk_3.sln b/2dgk_3/2dgk_3.sln new file mode 100644 index 0000000..38416b7 --- /dev/null +++ b/2dgk_3/2dgk_3.sln @@ -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("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2dgk_3", "2dgk_3\2dgk_3.vcxproj", "{1F080FDA-B417-497B-81D8-542963241D88}" +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 + {1F080FDA-B417-497B-81D8-542963241D88}.Debug|x64.ActiveCfg = Debug|x64 + {1F080FDA-B417-497B-81D8-542963241D88}.Debug|x64.Build.0 = Debug|x64 + {1F080FDA-B417-497B-81D8-542963241D88}.Debug|x86.ActiveCfg = Debug|Win32 + {1F080FDA-B417-497B-81D8-542963241D88}.Debug|x86.Build.0 = Debug|Win32 + {1F080FDA-B417-497B-81D8-542963241D88}.Release|x64.ActiveCfg = Release|x64 + {1F080FDA-B417-497B-81D8-542963241D88}.Release|x64.Build.0 = Release|x64 + {1F080FDA-B417-497B-81D8-542963241D88}.Release|x86.ActiveCfg = Release|Win32 + {1F080FDA-B417-497B-81D8-542963241D88}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1FA1F531-B810-4127-A45F-BA09DBA8E432} + EndGlobalSection +EndGlobal diff --git a/2dgk_3/2dgk_3/2dgk_3.vcxproj b/2dgk_3/2dgk_3/2dgk_3.vcxproj new file mode 100644 index 0000000..8184b62 --- /dev/null +++ b/2dgk_3/2dgk_3/2dgk_3.vcxproj @@ -0,0 +1,164 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {1f080fda-b417-497b-81d8-542963241d88} + My2dgk3 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + true + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x86-windows\x86-windows\include\SDL2 + + + Console + true + vcpkg_installed\x86-windows\x86-windows\debug\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2maind.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x86-windows\x86-windows\include\SDL2 + + + Console + true + true + true + vcpkg_installed\x86-windows\x86-windows\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2main.lib;%(AdditionalDependencies) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x86-windows\x86-windows\include\SDL2;%(AdditionalIncludeDirectories) + + + Console + true + vcpkg_installed\x64-windows\x64-windows\debug\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2maind.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + vcpkg_installed\x64-windows\x64-windows\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2main.lib;%(AdditionalDependencies) + + + + + + + + + + + + \ No newline at end of file diff --git a/2dgk_3/2dgk_3/2dgk_3.vcxproj.filters b/2dgk_3/2dgk_3/2dgk_3.vcxproj.filters new file mode 100644 index 0000000..a71858a --- /dev/null +++ b/2dgk_3/2dgk_3/2dgk_3.vcxproj.filters @@ -0,0 +1,25 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + + \ No newline at end of file diff --git a/2dgk_3/2dgk_3/level.txt b/2dgk_3/2dgk_3/level.txt new file mode 100644 index 0000000..41ca7c7 --- /dev/null +++ b/2dgk_3/2dgk_3/level.txt @@ -0,0 +1,31 @@ +/------------\/------------\ +|............||............| +|./--\./---\.||./---\./--\.| +|#| |.| |.||.| |.| |#| +|.`--'.`---'.`'.`---'.`--'.| +|..........................| +|./--\./\./------\./\./--\.| +|.`--'.||.`--\/--'.||.`--'.| +|......||....||....||......| +`----\.|`--\ || /--'|./----' + |.|/--' `' `--\|.| + |.|| ||.| + |.|| /--==--\ ||.| +-----'.`' | | `'.`----- + . | | . +-----\./\ | | /\./----- + |.|| `------' ||.| + |.|| ||.| + |.|| /------\ ||.| +/----'.`' `--\/--' `'.`----\ +|............||............| +|./--\./---\.||./---\./--\.| +|.`-\|.`---'.`'.`---'.|/-'.| +|#..||....... .......||..#| +`-\.||./\./------\./\.||./-' +/-'.`'.||.`--\/--'.||.`'.`-\ +|......||....||....||......| +|./----'`--\.||./--'`----\.| +|.`--------'.`'.`--------'.| +|..........................| +`--------------------------' \ No newline at end of file diff --git a/2dgk_3/2dgk_3/main.cpp b/2dgk_3/2dgk_3/main.cpp new file mode 100644 index 0000000..2b75609 --- /dev/null +++ b/2dgk_3/2dgk_3/main.cpp @@ -0,0 +1,464 @@ +//Using SDL and standard IO +#include "SDL.h" +#include +#include +#include +#include + +//Screen dimension constants +const int SCREEN_WIDTH = 896; +const int SCREEN_HEIGHT = 1024; +const int TILE_WIDTH = 32; +const int TILE_HEIGHT = 32; +const float SPEED = 0.16f; +const float SMOOTH = 0.5f; + +enum TILE_TYPE { + DEFAULT = 0, + HORIZONTAL_WALL = 1, + VERTICAL_WALL = 2, + DOT = 3 +}; + +class Vector2D { +public: + + Vector2D(float x, float y) : m_x(x), m_y(y) {} + float getX() { return m_x; } + float getY() { return m_y; } + + void setX(float x) { m_x = x; } + void setY(float y) { m_y = y; } + + float length() { return sqrt(m_x * m_x + m_y * m_y); } + + Vector2D operator+(const Vector2D& v2) const { + return Vector2D(m_x + v2.m_x, m_y + v2.m_y); + } + + friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2) { + v1.m_x += v2.m_x; + v1.m_y += v2.m_y; + return v1; + } + + Vector2D operator*(float scalar) { + return Vector2D(m_x * scalar, m_y * scalar); + } + + Vector2D& operator*=(float scalar) { + m_x *= scalar; + m_y *= scalar; + return *this; + } + + Vector2D operator-(const Vector2D& v2) const { + return Vector2D(m_x - v2.m_x, m_y - v2.m_y); + } + friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2) { + v1.m_x -= v2.m_x; + v1.m_y -= v2.m_y; + return v1; + } + + Vector2D operator/(float scalar) { + return Vector2D(m_x / scalar, m_y / scalar); + } + Vector2D& operator/=(float scalar) { + m_x /= scalar; + m_y /= scalar; + return *this; + } + + void normalize() { + float l = length(); + if (l > 0) { + (*this) *= 1 / l; + } + } + +private: + + float m_x; + float m_y; +}; + +class KTile { +public: + //Initializes position and type + KTile(int x, int y, TILE_TYPE tileType); + + //Shows the tile + void render(); + + //Get the tile type + int getType(); + + SDL_Rect getBox(); + +private: + //The attributes of the tile + SDL_Rect mBox; + + //The tile type + TILE_TYPE mType; +}; + +int KTile::getType() +{ + return mType; +} + +SDL_Rect KTile::getBox() +{ + return mBox; +} + +//Texture wrapper class +class KTexture +{ +public: + //Initializes variables + KTexture(); + + //Deallocates memory + ~KTexture(); + + //Loads image at specified path + bool loadFromFile(std::string path); + + //Deallocates texture + void free(); + + //Renders texture at given point + void render(int x, int y, SDL_Rect* clip = NULL); + + //Gets image dimensions + int getWidth(); + int getHeight(); + +private: + //The actual hardware texture + SDL_Texture* mTexture; + + //Image dimensions + int mWidth; + int mHeight; +}; + +SDL_Window* gWindow = NULL; + +SDL_Renderer* gRenderer = NULL; + +KTexture gTileTextures[4]; +std::vector gTiles; + +bool init(); + +bool loadMedia(); + +void close(); +SDL_Texture* loadTexture(std::string path); + +KTexture::KTexture() +{ + //Initialize + mTexture = NULL; + mWidth = 0; + mHeight = 0; +} + +KTexture::~KTexture() +{ + //Deallocate + free(); +} + +bool KTexture::loadFromFile(std::string path) +{ + //Get rid of preexisting texture + free(); + //The final texture + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + 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(gRenderer, loadedSurface); + if (newTexture == NULL) + { + printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Get image dimensions + mWidth = loadedSurface->w; + mHeight = loadedSurface->h; + } + + //Get rid of old loaded surface + SDL_FreeSurface(loadedSurface); + } + + //Return success + mTexture = newTexture; + return mTexture != NULL; +} + +void KTexture::free() +{ + //Free texture if it exists + if (mTexture != NULL) + { + SDL_DestroyTexture(mTexture); + mTexture = NULL; + mWidth = 0; + mHeight = 0; + } +} + +void KTexture::render(int x, int y, SDL_Rect* clip) +{ + //Set rendering space and render to screen + SDL_Rect renderQuad = { x, y, mWidth, mHeight }; + + //Set clip rendering dimensions + if (clip != NULL) + { + renderQuad.w = clip->w; + renderQuad.h = clip->h; + } + + //Render to screen + SDL_RenderCopy(gRenderer, mTexture, clip, &renderQuad); +} + +int KTexture::getWidth() +{ + return mWidth; +} + +int KTexture::getHeight() +{ + return mHeight; +} + +bool init() +{ + //Initialization flag + bool success = true; + + //Initialize SDL + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Set texture filtering to linear + if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) + { + printf("Warning: Linear texture filtering not enabled!"); + } + //Create window + gWindow = SDL_CreateWindow("2DGK - Zadanie 3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (gWindow == NULL) + { + printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Create renderer for window + gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); + if (gRenderer == NULL) + { + printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Initialize renderer color + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + } + } + } + + return success; +} + +bool loadMedia() +{ + //Loading success flag + bool success = true; + + for (int i = TILE_TYPE::DEFAULT; i <= TILE_TYPE::DOT; ++i) + { + if (!gTileTextures[i].loadFromFile("textures/0" + std::to_string(i) + ".bmp")) + { + printf("Failed to load 0%d texture image!\n", i); + success = false; + } + } + + gTiles.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)) { + for (int i = 0; i < line.length(); ++i) { + TILE_TYPE type = TILE_TYPE::DEFAULT; + switch (line[i]) + { + case '|': + type = TILE_TYPE::VERTICAL_WALL; + break; + case '-': + type = TILE_TYPE::HORIZONTAL_WALL; + break; + case '.': + type = TILE_TYPE::DOT; + break; + } + gTiles.push_back(KTile(i*TILE_WIDTH, y*TILE_HEIGHT, type)); + } + ++y; + } + levelFile.close(); + } + + return success; +} + +SDL_Texture* loadTexture(std::string path) +{ + //The final optimized image + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Create texture from surface pixels + newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface); + if (newTexture == NULL) + { + 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; +} + +void close() +{ + //Free loaded images + for (int i = 0; i <= TILE_TYPE::DOT; ++i) + gTileTextures[i].free(); + + //Destroy window + SDL_DestroyRenderer(gRenderer); + SDL_DestroyWindow(gWindow); + gWindow = NULL; + gRenderer = NULL; + + //Quit SDL subsystems + SDL_Quit(); +} + +int main(int argc, char* args[]) +{ + //Start up SDL and create window + if (!init()) + { + printf("Failed to initialize!\n"); + } + else + { + //Load media + if (!loadMedia()) + { + printf("Failed to load media!\n"); + } + else + { + //Main loop flag + bool quit = false; + + //Event handler + SDL_Event e; + + //While application is running + while (!quit) + {//Handle events on queue + while (SDL_PollEvent(&e) != 0) + { + //User requests quit + if (e.type == SDL_QUIT) + { + quit = true; + } + } + + //Clear screen + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + SDL_RenderClear(gRenderer); + + + for (auto& tile : gTiles) + tile.render(); + + //Update screen + SDL_RenderPresent(gRenderer); + } + } + } + + //Free resources and close SDL + close(); + + return 0; +} + +KTile::KTile(int x, int y, TILE_TYPE tileType) +{ + //Get the offsets + mBox.x = x; + mBox.y = y; + + mBox.w = TILE_WIDTH; + mBox.h = TILE_HEIGHT; + + //Get the tile type + mType = tileType; + +} + +void KTile::render() +{ + gTileTextures[mType].render(mBox.x, mBox.y); +} diff --git a/2dgk_3/2dgk_3/textures/00.bmp b/2dgk_3/2dgk_3/textures/00.bmp new file mode 100644 index 0000000..34f892e --- /dev/null +++ b/2dgk_3/2dgk_3/textures/00.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f46f203f44733ef973f3231b899951e6ab581739a57f3a3bab008ae5fe379711 +size 3126 diff --git a/2dgk_3/2dgk_3/textures/01.bmp b/2dgk_3/2dgk_3/textures/01.bmp new file mode 100644 index 0000000..563e1e1 --- /dev/null +++ b/2dgk_3/2dgk_3/textures/01.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:77efffc116983d8bc47f1866823f050823e035590f8bea75a050de01e052f0d5 +size 3126 diff --git a/2dgk_3/2dgk_3/textures/02.bmp b/2dgk_3/2dgk_3/textures/02.bmp new file mode 100644 index 0000000..87bdec3 --- /dev/null +++ b/2dgk_3/2dgk_3/textures/02.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:744ed424d4794efdabecdfc0b8618d53de57f20b7229b12a0d1db40e1ecd9b30 +size 3126 diff --git a/2dgk_3/2dgk_3/textures/03.bmp b/2dgk_3/2dgk_3/textures/03.bmp new file mode 100644 index 0000000..adf47f9 --- /dev/null +++ b/2dgk_3/2dgk_3/textures/03.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5ca36925a5793836bd24dbf79fbaa05f153d542cd11d2237bf69d51cea0ee13d +size 3126 diff --git a/2dgk_3/2dgk_3/vcpkg.json b/2dgk_3/2dgk_3/vcpkg.json new file mode 100644 index 0000000..e5ea828 --- /dev/null +++ b/2dgk_3/2dgk_3/vcpkg.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", + "name": "dgk-3", + "version": "0.1.0", + "dependencies": [ + "sdl2" + ] +} \ No newline at end of file diff --git a/2dgk_4/2dgk_4.sln b/2dgk_4/2dgk_4.sln new file mode 100644 index 0000000..84d0050 --- /dev/null +++ b/2dgk_4/2dgk_4.sln @@ -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_4", "2dgk_4\2dgk_4.vcxproj", "{BA8CD0C1-EABA-4C22-8C4F-5FDBE63A5744}" +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 + {BA8CD0C1-EABA-4C22-8C4F-5FDBE63A5744}.Debug|x64.ActiveCfg = Debug|x64 + {BA8CD0C1-EABA-4C22-8C4F-5FDBE63A5744}.Debug|x64.Build.0 = Debug|x64 + {BA8CD0C1-EABA-4C22-8C4F-5FDBE63A5744}.Debug|x86.ActiveCfg = Debug|Win32 + {BA8CD0C1-EABA-4C22-8C4F-5FDBE63A5744}.Debug|x86.Build.0 = Debug|Win32 + {BA8CD0C1-EABA-4C22-8C4F-5FDBE63A5744}.Release|x64.ActiveCfg = Release|x64 + {BA8CD0C1-EABA-4C22-8C4F-5FDBE63A5744}.Release|x64.Build.0 = Release|x64 + {BA8CD0C1-EABA-4C22-8C4F-5FDBE63A5744}.Release|x86.ActiveCfg = Release|Win32 + {BA8CD0C1-EABA-4C22-8C4F-5FDBE63A5744}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D3F726A0-2482-43D2-84D3-6405779AC32F} + EndGlobalSection +EndGlobal diff --git a/2dgk_4/2dgk_4/2dgk_4.vcxproj b/2dgk_4/2dgk_4/2dgk_4.vcxproj new file mode 100644 index 0000000..9a47c9c --- /dev/null +++ b/2dgk_4/2dgk_4/2dgk_4.vcxproj @@ -0,0 +1,165 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {ba8cd0c1-eaba-4c22-8c4f-5fdbe63a5744} + My2dgk3 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + true + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x86-windows\x86-windows\include\SDL2 + + + Console + true + vcpkg_installed\x86-windows\x86-windows\debug\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2maind.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x86-windows\x86-windows\include\SDL2 + + + Console + true + true + true + vcpkg_installed\x86-windows\x86-windows\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2main.lib;%(AdditionalDependencies) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x64-windows\x64-windows\include\SDL2 + + + Console + true + vcpkg_installed\x64-windows\x64-windows\debug\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2maind.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + vcpkg_installed\x64-windows\x64-windows\include\SDL2 + + + Console + true + true + true + vcpkg_installed\x64-windows\x64-windows\lib\manual-link;%(AdditionalLibraryDirectories) + SDL2main.lib;%(AdditionalDependencies) + + + + + + + + + + + + \ No newline at end of file diff --git a/2dgk_4/2dgk_4/2dgk_4.vcxproj.filters b/2dgk_4/2dgk_4/2dgk_4.vcxproj.filters new file mode 100644 index 0000000..a71858a --- /dev/null +++ b/2dgk_4/2dgk_4/2dgk_4.vcxproj.filters @@ -0,0 +1,25 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + + \ No newline at end of file diff --git a/2dgk_4/2dgk_4/level.txt b/2dgk_4/2dgk_4/level.txt new file mode 100644 index 0000000..41ca7c7 --- /dev/null +++ b/2dgk_4/2dgk_4/level.txt @@ -0,0 +1,31 @@ +/------------\/------------\ +|............||............| +|./--\./---\.||./---\./--\.| +|#| |.| |.||.| |.| |#| +|.`--'.`---'.`'.`---'.`--'.| +|..........................| +|./--\./\./------\./\./--\.| +|.`--'.||.`--\/--'.||.`--'.| +|......||....||....||......| +`----\.|`--\ || /--'|./----' + |.|/--' `' `--\|.| + |.|| ||.| + |.|| /--==--\ ||.| +-----'.`' | | `'.`----- + . | | . +-----\./\ | | /\./----- + |.|| `------' ||.| + |.|| ||.| + |.|| /------\ ||.| +/----'.`' `--\/--' `'.`----\ +|............||............| +|./--\./---\.||./---\./--\.| +|.`-\|.`---'.`'.`---'.|/-'.| +|#..||....... .......||..#| +`-\.||./\./------\./\.||./-' +/-'.`'.||.`--\/--'.||.`'.`-\ +|......||....||....||......| +|./----'`--\.||./--'`----\.| +|.`--------'.`'.`--------'.| +|..........................| +`--------------------------' \ No newline at end of file diff --git a/2dgk_4/2dgk_4/main.cpp b/2dgk_4/2dgk_4/main.cpp new file mode 100644 index 0000000..b2afb6d --- /dev/null +++ b/2dgk_4/2dgk_4/main.cpp @@ -0,0 +1,537 @@ +//Using SDL and standard IO +#include "SDL.h" +#include +#include +#include +#include + +//Screen dimension constants +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; +const int TILE_WIDTH = 32; +const int TILE_HEIGHT = 32; +const float SPEED = 0.16f; +const float SMOOTH = 0.4f; + +enum TILE_TYPE { + DEFAULT = 0, + HORIZONTAL_WALL = 1, + VERTICAL_WALL = 2, + DOT = 3 +}; + +class Vector2D { +public: + + Vector2D(float x, float y) : m_x(x), m_y(y) {} + float getX() { return m_x; } + float getY() { return m_y; } + + void setX(float x) { m_x = x; } + void setY(float y) { m_y = y; } + + float length() { return sqrt(m_x * m_x + m_y * m_y); } + + Vector2D operator+(const Vector2D& v2) const { + return Vector2D(m_x + v2.m_x, m_y + v2.m_y); + } + + friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2) { + v1.m_x += v2.m_x; + v1.m_y += v2.m_y; + return v1; + } + + Vector2D operator*(float scalar) { + return Vector2D(m_x * scalar, m_y * scalar); + } + + Vector2D& operator*=(float scalar) { + m_x *= scalar; + m_y *= scalar; + return *this; + } + + Vector2D operator-(const Vector2D& v2) const { + return Vector2D(m_x - v2.m_x, m_y - v2.m_y); + } + friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2) { + v1.m_x -= v2.m_x; + v1.m_y -= v2.m_y; + return v1; + } + + Vector2D operator/(float scalar) { + return Vector2D(m_x / scalar, m_y / scalar); + } + Vector2D& operator/=(float scalar) { + m_x /= scalar; + m_y /= scalar; + return *this; + } + + void normalize() { + float l = length(); + if (l > 0) { + (*this) *= 1 / l; + } + } + +private: + + float m_x; + float m_y; +}; + +class KTile { +public: + //Initializes position and type + KTile(int x, int y, TILE_TYPE tileType); + + //Shows the tile + void render(SDL_Rect& camera); + + //Get the tile type + int getType(); + + SDL_Rect getBox(); + +private: + //The attributes of the tile + SDL_Rect mBox; + + //The tile type + TILE_TYPE mType; +}; + +int KTile::getType() +{ + return mType; +} + +SDL_Rect KTile::getBox() +{ + return mBox; +} + +//Texture wrapper class +class KTexture +{ +public: + //Initializes variables + KTexture(); + + //Deallocates memory + ~KTexture(); + + //Loads image at specified path + bool loadFromFile(std::string path); + + //Deallocates texture + void free(); + + //Renders texture at given point + void render(int x, int y, SDL_Rect* clip = NULL); + + //Gets image dimensions + int getWidth(); + int getHeight(); + +private: + //The actual hardware texture + SDL_Texture* mTexture; + + //Image dimensions + int mWidth; + int mHeight; +}; + +SDL_Window* gWindow = NULL; + +SDL_Renderer* gRenderer = NULL; + +KTexture gTileTextures[4]; +KTexture gPlayerTexture; +std::vector gTiles; + +int gMapHeight = -1; +int gMapWidth = -1; + +bool init(); + +bool loadMedia(); + +void close(); +SDL_Texture* loadTexture(std::string path); + +KTexture::KTexture() +{ + //Initialize + mTexture = NULL; + mWidth = 0; + mHeight = 0; +} + +KTexture::~KTexture() +{ + //Deallocate + free(); +} + +bool KTexture::loadFromFile(std::string path) +{ + //Get rid of preexisting texture + free(); + //The final texture + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + 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(gRenderer, loadedSurface); + if (newTexture == NULL) + { + printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Get image dimensions + mWidth = loadedSurface->w; + mHeight = loadedSurface->h; + } + + //Get rid of old loaded surface + SDL_FreeSurface(loadedSurface); + } + + //Return success + mTexture = newTexture; + return mTexture != NULL; +} + +void KTexture::free() +{ + //Free texture if it exists + if (mTexture != NULL) + { + SDL_DestroyTexture(mTexture); + mTexture = NULL; + mWidth = 0; + mHeight = 0; + } +} + +void KTexture::render(int x, int y, SDL_Rect* clip) +{ + //Set rendering space and render to screen + SDL_Rect renderQuad = { x, y, mWidth, mHeight }; + + //Set clip rendering dimensions + if (clip != NULL) + { + renderQuad.w = clip->w; + renderQuad.h = clip->h; + } + + //Render to screen + SDL_RenderCopy(gRenderer, mTexture, clip, &renderQuad); +} + +int KTexture::getWidth() +{ + return mWidth; +} + +int KTexture::getHeight() +{ + return mHeight; +} + +bool init() +{ + //Initialization flag + bool success = true; + + //Initialize SDL + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Set texture filtering to linear + if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) + { + printf("Warning: Linear texture filtering not enabled!"); + } + //Create window + gWindow = SDL_CreateWindow("2DGK - Zadanie 4", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (gWindow == NULL) + { + printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Create renderer for window + gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED); + if (gRenderer == NULL) + { + printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); + success = false; + } + else + { + //Initialize renderer color + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + } + } + } + + return success; +} + +bool loadMedia() +{ + //Loading success flag + bool success = true; + + if (!gPlayerTexture.loadFromFile("textures/player.bmp")) + { + printf("Failed to load player texture image!\n"); + success = false; + } + + for (int i = TILE_TYPE::DEFAULT; i <= TILE_TYPE::DOT; ++i) + { + if (!gTileTextures[i].loadFromFile("textures/0" + std::to_string(i) + ".bmp")) + { + printf("Failed to load 0%d texture image!\n", i); + success = false; + } + } + + gTiles.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 (gMapWidth < (int)(line.length() * TILE_WIDTH)) + gMapWidth = line.length() * TILE_WIDTH; + for (int i = 0; i < line.length(); ++i) { + TILE_TYPE type = TILE_TYPE::DEFAULT; + switch (line[i]) + { + case '|': + type = TILE_TYPE::VERTICAL_WALL; + break; + case '-': + type = TILE_TYPE::HORIZONTAL_WALL; + break; + case '.': + type = TILE_TYPE::DOT; + break; + } + gTiles.push_back(KTile(i * TILE_WIDTH, y * TILE_HEIGHT, type)); + } + ++y; + } + gMapHeight = y * TILE_HEIGHT; + levelFile.close(); + } + + return success; +} + +SDL_Texture* loadTexture(std::string path) +{ + //The final optimized image + SDL_Texture* newTexture = NULL; + + //Load image at specified path + SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); + if (loadedSurface == NULL) + { + printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); + } + else + { + //Create texture from surface pixels + newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface); + if (newTexture == NULL) + { + 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; +} + +void close() +{ + //Free loaded images + for (int i = 0; i <= TILE_TYPE::DOT; ++i) + gTileTextures[i].free(); + + gPlayerTexture.free(); + + //Destroy window + SDL_DestroyRenderer(gRenderer); + SDL_DestroyWindow(gWindow); + gWindow = NULL; + gRenderer = NULL; + + //Quit SDL subsystems + SDL_Quit(); +} + +int main(int argc, char* args[]) +{ + //Start up SDL and create window + if (!init()) + { + printf("Failed to initialize!\n"); + } + else + { + //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 = SCREEN_WIDTH / 2; + viewport.y = SCREEN_HEIGHT / 2; + viewport.w = SCREEN_WIDTH; + viewport.h = SCREEN_HEIGHT; + + Vector2D playerOnePosition(0, 0); + Vector2D playerOneInput(0, 0); + Vector2D playerOneVelocity(0, 0); + + uint32_t time, previous_time; + time = previous_time = SDL_GetTicks(); + + //While application is running + while (!quit) + { + previous_time = time; + time = SDL_GetTicks(); + playerOneInput.setX(0); + playerOneInput.setY(0); + + //Handle events on queue + while (SDL_PollEvent(&e) != 0) + { + //User requests quit + if (e.type == SDL_QUIT) + { + quit = true; + } + } + + const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL); + if (currentKeyStates[SDL_SCANCODE_W]) + { + playerOneInput.setY(-1); + } + if (currentKeyStates[SDL_SCANCODE_S]) + { + playerOneInput.setY(1); + } + if (currentKeyStates[SDL_SCANCODE_A]) + { + playerOneInput.setX(-1); + } + if (currentKeyStates[SDL_SCANCODE_D]) + { + playerOneInput.setX(1); + } + + playerOneInput.normalize(); + playerOneVelocity = playerOneInput * SPEED * (1 - SMOOTH) + playerOneVelocity * SMOOTH; + + playerOnePosition += playerOneVelocity; + + if (playerOnePosition.getX() < 0) playerOnePosition.setX(0); + if (playerOnePosition.getX() > gMapWidth - gPlayerTexture.getWidth()) playerOnePosition.setX(gMapWidth - gPlayerTexture.getWidth()); + if (playerOnePosition.getY() < 0) playerOnePosition.setY(0); + if (playerOnePosition.getY() > gMapHeight - gPlayerTexture.getHeight()) playerOnePosition.setY(gMapHeight - gPlayerTexture.getHeight()); + + viewport.x += (time - previous_time) * .5f * (playerOnePosition.getX() + gPlayerTexture.getWidth() / 2 - SCREEN_WIDTH / 2 - viewport.x); + viewport.y += (time - previous_time) * .5f * (playerOnePosition.getY() + gPlayerTexture.getHeight() / 2 - SCREEN_HEIGHT / 2 - viewport.y); + if (viewport.x < 0) + viewport.x = 0; + if (viewport.y < 0) + viewport.y = 0; + if (viewport.x > gMapWidth - viewport.w) + viewport.x = gMapWidth - viewport.w; + if (viewport.y > gMapHeight - viewport.h) + viewport.y = gMapHeight - viewport.h; + + //Clear screen + SDL_SetRenderDrawColor(gRenderer, 0, 0xFF, 0, 0xFF); + SDL_RenderClear(gRenderer); + + for (auto& tile : gTiles) + tile.render(viewport); + + gPlayerTexture.render(playerOnePosition.getX() - viewport.x, playerOnePosition.getY() - viewport.y); + + //Update screen + SDL_RenderPresent(gRenderer); + } + } + } + + //Free resources and close SDL + close(); + + return 0; +} + +KTile::KTile(int x, int y, TILE_TYPE tileType) +{ + //Get the offsets + mBox.x = x; + mBox.y = y; + + mBox.w = TILE_WIDTH; + mBox.h = TILE_HEIGHT; + + //Get the tile type + mType = tileType; + +} + +void KTile::render(SDL_Rect& camera) +{ + gTileTextures[mType].render(mBox.x - camera.x, mBox.y - camera.y); +} diff --git a/2dgk_4/2dgk_4/textures/00.bmp b/2dgk_4/2dgk_4/textures/00.bmp new file mode 100644 index 0000000..34f892e --- /dev/null +++ b/2dgk_4/2dgk_4/textures/00.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f46f203f44733ef973f3231b899951e6ab581739a57f3a3bab008ae5fe379711 +size 3126 diff --git a/2dgk_4/2dgk_4/textures/01.bmp b/2dgk_4/2dgk_4/textures/01.bmp new file mode 100644 index 0000000..563e1e1 --- /dev/null +++ b/2dgk_4/2dgk_4/textures/01.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:77efffc116983d8bc47f1866823f050823e035590f8bea75a050de01e052f0d5 +size 3126 diff --git a/2dgk_4/2dgk_4/textures/02.bmp b/2dgk_4/2dgk_4/textures/02.bmp new file mode 100644 index 0000000..87bdec3 --- /dev/null +++ b/2dgk_4/2dgk_4/textures/02.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:744ed424d4794efdabecdfc0b8618d53de57f20b7229b12a0d1db40e1ecd9b30 +size 3126 diff --git a/2dgk_4/2dgk_4/textures/03.bmp b/2dgk_4/2dgk_4/textures/03.bmp new file mode 100644 index 0000000..adf47f9 --- /dev/null +++ b/2dgk_4/2dgk_4/textures/03.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5ca36925a5793836bd24dbf79fbaa05f153d542cd11d2237bf69d51cea0ee13d +size 3126 diff --git a/2dgk_4/2dgk_4/textures/player.bmp b/2dgk_4/2dgk_4/textures/player.bmp new file mode 100644 index 0000000..ab193d5 --- /dev/null +++ b/2dgk_4/2dgk_4/textures/player.bmp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:71ab1fbb649b03c37d1aa219be3d1221ee6a4e6dd7c141bccf29a862de1cdd9a +size 1878 diff --git a/2dgk_4/2dgk_4/vcpkg.json b/2dgk_4/2dgk_4/vcpkg.json new file mode 100644 index 0000000..4f64151 --- /dev/null +++ b/2dgk_4/2dgk_4/vcpkg.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json", + "name": "dgk-4", + "version": "0.1.0", + "dependencies": [ + "sdl2" + ] +} \ No newline at end of file