Pārlūkot izejas kodu

添加项目文件。

DESKTOP-UA5NHRC\jlutt 5 gadi atpakaļ
vecāks
revīzija
550537e158
4 mainītis faili ar 328 papildinājumiem un 0 dzēšanām
  1. 31 0
      SDLHelloWorld.sln
  2. 135 0
      SDLHelloWorld.vcxproj
  3. 22 0
      SDLHelloWorld.vcxproj.filters
  4. 140 0
      main.cpp

+ 31 - 0
SDLHelloWorld.sln

@@ -0,0 +1,31 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 15
+VisualStudioVersion = 15.0.28307.1209
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SDLHelloWorld", "SDLHelloWorld.vcxproj", "{23C14F9A-B580-4302-81DB-FB5CFC17CD63}"
+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
+		{23C14F9A-B580-4302-81DB-FB5CFC17CD63}.Debug|x64.ActiveCfg = Debug|x64
+		{23C14F9A-B580-4302-81DB-FB5CFC17CD63}.Debug|x64.Build.0 = Debug|x64
+		{23C14F9A-B580-4302-81DB-FB5CFC17CD63}.Debug|x86.ActiveCfg = Debug|Win32
+		{23C14F9A-B580-4302-81DB-FB5CFC17CD63}.Debug|x86.Build.0 = Debug|Win32
+		{23C14F9A-B580-4302-81DB-FB5CFC17CD63}.Release|x64.ActiveCfg = Release|x64
+		{23C14F9A-B580-4302-81DB-FB5CFC17CD63}.Release|x64.Build.0 = Release|x64
+		{23C14F9A-B580-4302-81DB-FB5CFC17CD63}.Release|x86.ActiveCfg = Release|Win32
+		{23C14F9A-B580-4302-81DB-FB5CFC17CD63}.Release|x86.Build.0 = Release|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {6D96A71D-A1BB-4F97-BF07-81A46CFE6271}
+	EndGlobalSection
+EndGlobal

+ 135 - 0
SDLHelloWorld.vcxproj

@@ -0,0 +1,135 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="15.0" 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>15.0</VCProjectVersion>
+    <ProjectGuid>{23C14F9A-B580-4302-81DB-FB5CFC17CD63}</ProjectGuid>
+    <RootNamespace>SDLHelloWorld</RootNamespace>
+    <WindowsTargetPlatformVersion>10.0.17763.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>v141</PlatformToolset>
+    <CharacterSet>MultiByte</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <PlatformToolset>v141</PlatformToolset>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>MultiByte</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <PlatformToolset>v141</PlatformToolset>
+    <CharacterSet>MultiByte</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <PlatformToolset>v141</PlatformToolset>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>MultiByte</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 />
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <SDLCheck>true</SDLCheck>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <SDLCheck>true</SDLCheck>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+    </Link>
+    <PreBuildEvent>
+      <Command>
+      </Command>
+    </PreBuildEvent>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <SDLCheck>true</SDLCheck>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <SDLCheck>true</SDLCheck>
+      <ConformanceMode>true</ConformanceMode>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClCompile Include="main.cpp" />
+  </ItemGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>

+ 22 - 0
SDLHelloWorld.vcxproj.filters

@@ -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="源文件">
+      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+    </Filter>
+    <Filter Include="头文件">
+      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+      <Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
+    </Filter>
+    <Filter Include="资源文件">
+      <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>源文件</Filter>
+    </ClCompile>
+  </ItemGroup>
+</Project>

+ 140 - 0
main.cpp

@@ -0,0 +1,140 @@
+#include <stdio.h>
+#include <SDL.h>
+#include <SDL_image.h>  //这里直接使用SDL_image库,可以加载如.jpg .png等各种格式的图片
+//使用OutputDebugString
+#include "atlbase.h" 
+#include "atlstr.h"
+
+// 定义窗口宽高
+const int SCREEN_WIDTH = 640;
+const int SCREEN_HEIGHT = 480;
+
+SDL_Window *g_pWindow = NULL;
+SDL_Renderer *g_pRenderer = NULL;
+SDL_Texture *g_pTexture = NULL;
+
+//=========================================================
+//函数名称:Init
+//说明:完成SDL初始化、创建窗口等
+//=========================================================
+bool Init()
+{
+	//(1)初始化所有SDL的各模块
+	if (SDL_Init(SDL_INIT_VIDEO) < 0)   //这里可以初始化SDL的各种模块,包括定时器,视频,音频、摇杆等
+						//实际项目中一般只初始化要用到的模块即可;成功返回0
+											//具体有哪些模块可以通过在SDL_INIT_VIDEO上点击右键-转到定义来查看
+	{
+		printf("Unable to Init SDL: %s", SDL_GetError());  //调用SDL_GetError()来查看失败原因
+		return false;
+	}
+
+	//(2)使用SDL创建窗口
+	g_pWindow = SDL_CreateWindow("Chapter 1: Hello World!", SDL_WINDOWPOS_CENTERED,
+		SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
+	if (g_pWindow == NULL)
+	{
+		printf("Unable to Create Window: %s", SDL_GetError());
+		return false;
+	}
+
+	//(3)初始化渲染器(renderer),用于在窗口绘图
+	g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
+	if (g_pRenderer == NULL)
+	{
+		printf("Unable to Create Renderer: %s", SDL_GetError());
+		return false;
+	}
+
+	return true;
+}
+
+//=========================================================
+//函数名称:LoadImage
+//说明:加载图片
+//=========================================================
+bool LoadImage(const char* filename)
+{
+	const char *error;
+	if (filename == NULL)
+	{
+		printf("LoadImage() error: filename is NULL!\n");
+		return false;
+	}
+
+	SDL_Surface *pTempSurface = IMG_Load(filename); //加载启动图像(splash image)到SDL_Surface对象
+	if (pTempSurface == NULL)
+	{
+		error = SDL_GetError();
+		printf("Unable to load image! SDL Error: %s\n", SDL_GetError());
+		return false;
+	}
+	g_pTexture = SDL_CreateTextureFromSurface(g_pRenderer, pTempSurface); //使用SDL_Surface对象创建纹理(texture)
+	if (g_pTexture == NULL)
+	{
+		printf("Unable to create texture from surface! SDL Error: %s\n", SDL_GetError());
+		return false;
+	}
+
+	SDL_FreeSurface(pTempSurface);  //回收surface对象占用的内存空间
+	return true;
+}
+
+//=========================================================
+//函数名称:Close
+//说明:销毁各对象
+//=========================================================
+void Close()
+{
+	//销毁窗口对象
+	SDL_DestroyWindow(g_pWindow);
+	g_pWindow = NULL;
+
+	//销毁渲染器对象
+	SDL_DestroyRenderer(g_pRenderer);
+	g_pRenderer = NULL;
+
+	//销毁纹理对象
+	SDL_DestroyTexture(g_pTexture);
+	g_pTexture = NULL;
+
+	//清除所有已初始化的子系统。在程序结束时调用。
+	SDL_Quit();
+}
+
+
+//=========================================================
+//函数名称:main
+//说明:主函数
+//=========================================================
+int main(int argc, char* argv[]) //注意这里必须在main的括号内加上int argc, char* argv[]否则报错"LNK2019无法解析的外部符号 _SDL_main"
+{
+	//Step1:初始化SDL
+	if (!Init())
+	{
+		printf("Failed to Init!\n");
+		return -1;
+	}
+
+	//Step2:加载图片
+	if (!LoadImage("D:\\Work\\SDLHelloWorld\\Debug\\helloworld.jpg"))
+	{
+		printf("Failed to load media!\n");
+		return -2;
+	}
+
+	SDL_SetRenderDrawColor(g_pRenderer, 255, 255, 255, 0);  //设置渲染器的颜色,第一个参数传入渲染器对象
+															//接下来依次是R、G、B值和Alpha值
+
+	//Step3:绘制图片三板斧
+	SDL_RenderClear(g_pRenderer);  //使用某种绘图颜色来“清除”当前窗口
+	SDL_RenderCopy(g_pRenderer, g_pTexture, NULL, NULL); //将纹理复制到渲染器中;最后两个参数的使用详见下一篇教程
+	SDL_RenderPresent(g_pRenderer);  //将渲染器中的内容在窗口中显示出来
+
+	OutputDebugString("Failed to load media!\n");
+	SDL_Delay(3000); //在程序结束之前延迟一段时间,以便观察窗口;实际项目中慎用!
+
+	//Step4:释放内存资源并关闭SDL
+	Close();
+
+	return 0;
+}