又到了一年一度的铁人赛啦~?!今年决定尝试用C与OpenGL写出一个2D平台卷轴游戏,希望至少一可以完成一个小关卡。已经可以预想到会碰到很多问题了,百分之百碰一个问题不会在一天就解决,所以如果要完成的话应该会不只30天,就看我可以撑到甚麽时候了。
我会尽量不漏地,一步一步叙述我的作法,有兴趣的读者可以跟着试试看。废话不多说,进入正题~
虽然我是用C与OpenGL制作,Windows、MacOS、Linux都可以进行跨平台建置,但目前只会专注在Windows平台
编辑器(Editor): Visual Studio Code
编译器(Compiler): GCC 10.3.0
CMake: 3.21.0-rc3
|
+---- external/ // 放第三方的libs
|
+---- src/ // 游戏sourece code
|
+---- CMakeLists.txt // CMake的建置脚本,名字不可变
这里选用GLFW管理专案的视窗生成与输入控制等功能,先把GLFW的专案整的下载下来後,把整包放到external资料夹底下,很快就会用到。
|
+--- external/
|
+---- glfw/
提问: 为什麽不使用SDL或是更土炮的呼叫WinMain呢?
主要原因是我OpenGL的相关知识都来自这个网站 - LearnOpenGL(https://learnopengl.com/),去年也有看到很多铁人赛赛友利用这个优良的学习网站撰写学习笔记,里面Create a Window这一章节就是用GLFW。
GLFW这个开源专案有须多贡献者,以及完善的文件,这里是他的官网,右上方Documentaion点下去,就会看到关键的Example Code:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
为什麽特地要说这个呢?原因我在初学的时候,找到一个看起来可以用的lib,直接二话不说Google或上Stackoverflow找相关范例,以及一些使用中遇到的错误,其实这些东西,往往都已经写在官方的wiki或官网上了,甚至有完整的流程与解说,有些甚至就直接所在原始码的注解里面了。完全不用绕这麽多路
上面这段范例,我会放在以下地方
|
+--- src/
|
+---- iron.h
|
+---- iron_main.c
然後这里是内容
// iron.h --> 这里之後会集中放相关的header
#ifndef _IRON_H_
#define _IRON_H_
#include "GLFW/glfw3.h"
#endif // _IRON_H_
// iron_main.c --> 2d platfomer的main.c
#include "iron.h"
// #include <stdio.h>
int main() {
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
// glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
return 0;
}
常常会看到一些开源的C或C++的专案有一个或多个CMakeList.txt
的档案,这些都是CMake的建置脚本。
这麽做的原因在於C与C++有相当多的实作平台,建置方法也不一,CMake帮我们做的事情,并不是建置,而是生成相对应平台的建置文件,例如: GCC或Clang的话就是Makefile。
但是CMake最麻烦的还是需要额外去学习CMake专用的语言,个人认为门槛挺高的,且只能在CMake使用。
介绍完毕,下面直接贴上我的2D Platformer的CMake脚本,注解会写上我知道但可能不适那麽准确的解释:
# cmake的注解是从'#'开始的
cmake_minimum_required(VERSION 3.1) # --- 最低版本 ---
project(LittleIron) # --- 专案名称 ---
#变数所有src资料夹中的source files
file(GLOB sources src/*.c)
# 不需要建置docs, examples, tests
# from - https://www.glfw.org/docs/latest/compile_guide.html#compile_options_shared
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_executable( # 标记执行党和原始码
LittleIron
${sources}
)
add_subdirectory(external/glfw) # 引用由glfw建置的lib
include_directories(external/glfw/include)
target_link_libraries( # 与glfw lib进行连接
LittleIron
glfw
)
接下来可以直接依靠VS Code插件提供的方便。
下图是我的VS Code,左牌工具列最下面的就是插件提供的建置按钮,如果没有看到的话,可以按下Ctrl + Shift + P
,输入CMake Quick Start,选择要使用的编译器後,就会开始设定专案的设定了,这时会发现多了一个build
的资料夹。
设定完之後红色框框的按钮按下去,就会开始建置了(如果正常的话... :)),最後会看到build
资料夹有LittleIron
(小铁)的执行档了。
<<: Day11. 从日常看到Blue Prism的窝心-BP合并两张报表
>>: Day 0xA UVa490 Rotating Sentences
彩色照片转黑白 教学原文参考:彩色照片转黑白 这篇文章会介绍使用 GIMP 的颜色调整功能,将一张彩...
今天要介绍的是legion,根据官方介绍,它是斯巴达的叉子 Legion, a fork of SE...
URL : https://app.hackthebox.eu/machines/3 IP : 1...
-资讯安全 安全是指保护某物免受危险或威胁的过程和达到的状态。 资讯安全是一门通过安全控制保护资讯...
DAY19 用 Azure Machine Learning SDK 建立 Datastore 我们...