Zadania 1-4
This commit is contained in:
31
2dgk_1/2dgk_1.sln
Normal file
31
2dgk_1/2dgk_1.sln
Normal file
@ -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
|
159
2dgk_1/2dgk_1/2dgk_1.vcxproj
Normal file
159
2dgk_1/2dgk_1/2dgk_1.vcxproj
Normal file
@ -0,0 +1,159 @@
|
||||
<?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>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{879303a0-7139-4efa-97eb-8e6ae360ee92}</ProjectGuid>
|
||||
<RootNamespace>My2dgk1</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>
|
||||
<LibraryPath>C:\vcpkg\installed\x86-windows\debug\lib\manual-link;$(LibraryPath)</LibraryPath>
|
||||
<IncludePath>C:\vcpkg\installed\x86-windows\include\SDL2;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<LibraryPath>C:\vcpkg\installed\x86-windows\lib\manual-link;$(LibraryPath)</LibraryPath>
|
||||
<IncludePath>C:\vcpkg\installed\x86-windows\include\SDL2;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<VcpkgInstalledDir>..\..\..\..\..\..\vcpkg\installed</VcpkgInstalledDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Vcpkg" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<VcpkgInstalledDir>..\..\..\..\..\..\vcpkg\installed</VcpkgInstalledDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<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>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<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>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</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>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
22
2dgk_1/2dgk_1/2dgk_1.vcxproj.filters
Normal file
22
2dgk_1/2dgk_1/2dgk_1.vcxproj.filters
Normal file
@ -0,0 +1,22 @@
|
||||
<?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>
|
||||
</ItemGroup>
|
||||
</Project>
|
351
2dgk_1/2dgk_1/main.cpp
Normal file
351
2dgk_1/2dgk_1/main.cpp
Normal file
@ -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 <SDL.h>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
//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;
|
||||
}
|
BIN
2dgk_1/2dgk_1/textures/01.bmp
(Stored with Git LFS)
Normal file
BIN
2dgk_1/2dgk_1/textures/01.bmp
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
2dgk_1/2dgk_1/textures/02.bmp
(Stored with Git LFS)
Normal file
BIN
2dgk_1/2dgk_1/textures/02.bmp
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
2dgk_1/2dgk_1/textures/sprite.bmp
(Stored with Git LFS)
Normal file
BIN
2dgk_1/2dgk_1/textures/sprite.bmp
(Stored with Git LFS)
Normal file
Binary file not shown.
Reference in New Issue
Block a user