fix: SDL2 Linux port - fix black screen by setting CWD to binary path

This commit is contained in:
2026-05-03 11:29:39 +08:00
parent 9b9cb9e86c
commit 897abcf068
8 changed files with 670 additions and 921 deletions
+13 -34
View File
@@ -7,6 +7,12 @@ project(tank_battles_on_the_scrap_paper
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# ── SDL2 依赖 ──
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_mixer REQUIRED)
find_package(SDL2_ttf REQUIRED)
# ── 源文件 ──
set(SOURCES
tank_battles_on_the_scrap_paper/main.cpp
@@ -18,45 +24,18 @@ set(HEADERS
tank_battles_on_the_scrap_paper/tank.h
tank_battles_on_the_scrap_paper/map.h
tank_battles_on_the_scrap_paper/data_config.h
tank_battles_on_the_scrap_paper/resource.h
)
# ── 可执行文件 ──
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS}
tank_battles_on_the_scrap_paper/resource.rc
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
target_link_libraries(${PROJECT_NAME} PRIVATE
SDL2::SDL2
SDL2_image::SDL2_image
SDL2_mixer::SDL2_mixer
SDL2_ttf::SDL2_ttf
)
# ── Windows 特定配置 ──
if(WIN32)
# EasyX 图形库
target_include_directories(${PROJECT_NAME} PRIVATE
${EASYX_INCLUDE_DIR}
)
# Windows 多媒体库
target_link_libraries(${PROJECT_NAME} PRIVATE
winmm.lib
gdi32.lib
)
# 控制台子系统
set_target_properties(${PROJECT_NAME} PROPERTIES
WIN32_EXECUTABLE FALSE
)
target_compile_definitions(${PROJECT_NAME} PRIVATE
_CONSOLE
_UNICODE
UNICODE
)
else()
# 非 Windows 平台给出提示
message(WARNING
"${PROJECT_NAME} 依赖 EasyX (Windows-only 图形库)"
"当前平台不支持编译。仅生成项目结构参考。"
)
endif()
# ── 复制资源文件到构建目录 ──
file(COPY
tank_battles_on_the_scrap_paper/images/
-31
View File
@@ -1,31 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33530.505
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tank_battles_on_the_scrap_paper", "tank_battles_on_the_scrap_paper\tank_battles_on_the_scrap_paper.vcxproj", "{AC8C4A04-1650-473C-8E9A-3B7CDE2AD5CC}"
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
{AC8C4A04-1650-473C-8E9A-3B7CDE2AD5CC}.Debug|x64.ActiveCfg = Debug|x64
{AC8C4A04-1650-473C-8E9A-3B7CDE2AD5CC}.Debug|x64.Build.0 = Debug|x64
{AC8C4A04-1650-473C-8E9A-3B7CDE2AD5CC}.Debug|x86.ActiveCfg = Debug|Win32
{AC8C4A04-1650-473C-8E9A-3B7CDE2AD5CC}.Debug|x86.Build.0 = Debug|Win32
{AC8C4A04-1650-473C-8E9A-3B7CDE2AD5CC}.Release|x64.ActiveCfg = Release|x64
{AC8C4A04-1650-473C-8E9A-3B7CDE2AD5CC}.Release|x64.Build.0 = Release|x64
{AC8C4A04-1650-473C-8E9A-3B7CDE2AD5CC}.Release|x86.ActiveCfg = Release|Win32
{AC8C4A04-1650-473C-8E9A-3B7CDE2AD5CC}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4E9B74CF-EE0B-430C-8380-34BE94639065}
EndGlobalSection
EndGlobal
+36 -37
View File
@@ -1,52 +1,51 @@
// 其他配置定义
// ö
#ifndef DATA_CONFIG_H
#define DATA_CONFIG_H
// 导入 tank bullet 定义头文件
// tank bullet ͷļ
#include "tank.h"
#include "bullet.h"
// 常量定义
const int CELL_SIZE = 30; // 每个单元格(坦克、墙、空地等)的大小(例如:像素)
const int MAP_CELL_NUM = 20; // 地图大小为MAP_CELL_NUM^2
const int ENEMIES_NUM = 10; // 地图上最多的敌人数量
const int BULLET_NUM = 20; // 地图上最多的子弹数量
const int SINGLE_BULLET = 3; // 每个坦克同时存在的最多子弹数量
const int White = 0; // 灰度图-白
const int Black = 1; // 灰度图-黑
//
const int CELL_SIZE = 30; // ÿԪ̹ˡǽյصȣĴСأ
const int MAP_CELL_NUM = 20; // ͼСΪMAP_CELL_NUM^2
const int ENEMIES_NUM = 10; // ͼĵ
const int BULLET_NUM = 20; // ͼӵ
const int SINGLE_BULLET = 3; // ÿ̹ͬʱڵӵ
const int White = 0; // Ҷͼ-
const int Black = 1; // Ҷͼ-
#define PIEXL1 30 //像素常量
#define PIEXL1 30 //س
#define PIEXL2 4
#define X_OFFSET 50 //以左上第一块墙为基准的偏移量
#define Y_OFFSET 45 //以左上第一块墙为基准的偏移量
#define X_OFFSET 50 //ϵһǽΪ׼ƫ
#define Y_OFFSET 45 //ϵһǽΪ׼ƫ
/*
地图:
使用数组存储地图信息
map[x][y] 存储当前坦克信息
BLANK 空地
RED_TANK 红方坦克
RED_DEAD_TANK 红方阵亡坦克
BLUE_TANK, 蓝方坦克
BLUE_DEAD_TANK 蓝方阵亡坦克
ͼ
ʹͼϢ
map[x][y] ǰ̹Ϣ
BLANK յ
RED_TANK 췽̹
RED_DEAD_TANK ̹
BLUE_TANK, ̹
BLUE_DEAD_TANK ̹
WALL ǽ
BULLET 子弹
BULLET ӵ
*/
enum CELL_Type {
BLANK, // 空地
RED_TANK, // 红方坦克
RED_DEAD_TANK, // 红方报废坦克
BLUE_TANK, // 蓝方坦克
BLUE_DEAD_TANK, // 蓝方报废坦克
BLANK, // յ
RED_TANK, // 췽̹
RED_DEAD_TANK, // ̹
BLUE_TANK, // ̹
BLUE_DEAD_TANK, // ̹
WALL, // ǽ
BULLET, // 子弹
BULLET, // ӵ
};
int map[MAP_CELL_NUM][MAP_CELL_NUM]; //在map.h预设好
int kill = 5; //击杀数
tank player; //玩家
tank enemies[ENEMIES_NUM]; //敌人
bullet E_bullets[BULLET_NUM * ENEMIES_NUM]; //敌方坦克子弹数组
bullet P_bullets[BULLET_NUM]; //玩家子弹数组
tank enemy; // 用于更改敌人的属性(难度设置用到)
// 全局变量声明(extern),定义在 main.cpp 中
extern int map[MAP_CELL_NUM][MAP_CELL_NUM];
extern int kill;
extern tank player;
extern tank enemies[ENEMIES_NUM];
extern bullet E_bullets[BULLET_NUM * ENEMIES_NUM];
extern bullet P_bullets[BULLET_NUM];
extern tank enemy;
#endif // !DATA_CONFIG_H
File diff suppressed because it is too large Load Diff
@@ -1,16 +0,0 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ 生成的包含文件。
// 供 resource.rc 使用
//
#define IDR_WAVE1 101
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
Binary file not shown.
@@ -1,147 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{ac8c4a04-1650-473c-8e9a-3b7cde2ad5cc}</ProjectGuid>
<RootNamespace>tankbattlesonthescrappaper</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>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</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" />
<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>
</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>
</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>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="bullet.h" />
<ClInclude Include="data_config.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="tank.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="resource.rc" />
</ItemGroup>
<ItemGroup>
<Media Include="music\bgm.wav" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
@@ -1,46 +0,0 @@
<?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;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;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>
<ItemGroup>
<ClInclude Include="tank.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="bullet.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="data_config.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="resource.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="resource.rc">
<Filter>资源文件</Filter>
</ResourceCompile>
</ItemGroup>
<ItemGroup>
<Media Include="music\bgm.wav">
<Filter>资源文件</Filter>
</Media>
</ItemGroup>
</Project>