文章目錄
- 1、簡介
- 1.1 先決條件
- 1.2 查找并安裝包
- 1.3 解除安裝軟體包
- 1.4 更新包
- 1.5 管了解決方案的包
- 1.6 “合并”頁籤
- 2、nuget配置程式源
- 2.1 線上源
- 2.2 本地源
- 3、nuget安裝庫
- 3.1 nuget安裝nupengl.core(C++)
- 3.2 nuget安裝Giawa.OpenGL(C#)
- 4、代碼測試
- 4.1 測試代碼:freeglut
- 4.2 測試代碼:freeglut+tea
- 4.3 測試代碼:freeglut+glew
- 4.4 測試代碼:glfw
- 4.5 測試代碼:glfw+glew
- 4.6 測試代碼:glfw+glad
- 4.7 測試代碼:glfw+glad+shader
- 4.8 測試代碼:sdl2
- 4.9 測試代碼:sdl2+glew
- 4.10 測試代碼:sdl2+glad
- 結語
1、簡介
https://www.nuget.org/
如果你了解python,那麼它類似pip。如果你了解nodejs,那麼它類似npm。
什麼是NuGet?
NuGet 是 .NET 的包管理器。NuGet 用戶端工具提供生成和使用包的功能。NuGet 庫是所有包作者和使用者使用的中央包存儲庫。
NuGet它就是一個包(package)管理平台,确切的說是 .net平台的包管理工具,它提供了一系列用戶端用于生成,上傳和使用包(package),以及一個用于存儲所有包的中心庫即NuGet Gallery,如果有需要也可以搭建自己的私有NuGet庫。
适用于任何現代開發平台的基本工具可充當一種機制,通過這種機制,開發人員可以建立、共享和使用有用的代碼。 通常,此類代碼捆綁到“包”中,其中包含編譯的代碼(如 DLL)以及在使用這些包的項目中所需的其他内容。
NuGet 包是具有 .nupkg 擴充的單個 ZIP 檔案,此擴充包含編譯代碼 (Dll)、與該代碼相關的其他檔案以及描述性清單(包含包版本号等資訊)。 使用代碼的開發人員共享建立包,并将其釋出到公用或專用主機。 包使用者從适合的主機擷取這些包,将它們添加到項目,然後在其項目代碼中調用包的功能。 随後,NuGet 自身負責處理所有中間詳細資訊。
NuGet的用戶端融合在各類開發工具中,包括但不限于:
.net core SDK中的nuget指令行;
Visual Studio中的nuget工具;
nuget.exe 指令行用戶端;
Visual Studio Code中的nuget插件;
Microsoft Visual Studio for Windows 中的 NuGet 程式包管理器 UI 允許你輕松安裝、解除安裝和更新項目和解決方案中的 NuGet 程式包。
1.1 先決條件
- (1)Visual Studio 2017 及更高版本在 .已安裝與 NET 相關的工作負載。若要在 Visual Studio 安裝程式中單獨安裝它,請選擇“單個元件”頁籤,然後在“代碼工具”下選擇“NuGet 包管理器”。
- (2)對于 Visual Studio 2015,如果缺少 NuGet 包管理器,請檢查“擴充和更新>工具”,然後搜尋 NuGet 包管理器擴充。如果無法在 Visual Studio 中使用擴充安裝程式,請直接從 https://dist.nuget.org/index.html 下載下傳擴充。
- (3)如果您還沒有免費帳戶,請在 nuget.org 上注冊一個免費帳戶。必須先注冊并确認帳戶,然後才能上傳 NuGet 包。
1.2 查找并安裝包
若要查找并安裝 NuGet 包與 Visual Studio,請按照下列步驟操作:
- 在“解決方案資料總管”中附加元件目,然後選擇“項目”>“管理 NuGet 包”。
- 将打開“NuGet 包管理器”視窗。
-
選擇“浏覽”頁籤以按目前所選源的受歡迎程度顯示包(請參閱包源)。要搜尋特定包,請使用左上角的搜尋框。從清單中選擇一個包,以在右窗格中顯示其資訊,這樣您就可以選擇要安裝的版本。
若要使用 NuGet 包管理器 在 Visual Studio 中安裝 Newtonsoft.Json 包,請執行以下步驟:
(1)選擇“項目”>“管理 NuGet 包” 。
(2)在“NuGet 包管理器”視窗中,選擇“nuget.org”作為包源。
(3)在 “浏覽 ”頁籤中,搜尋 Newtonsoft.Json,在清單中選擇 Newtonsoft.Json ,然後選擇“ 安裝”。
(4)如果系統提示驗證安裝,請選擇“ 确定”。
1.3 解除安裝軟體包
若要解除安裝 NuGet 包,請執行以下步驟:
- 在“解決方案資料總管”中附加元件目,選擇“項目”>“管理 NuGet 包”,然後選擇“已安裝”頁籤。
- 在左窗格中選擇要解除安裝的包(如有必要,請使用“搜尋”框查找它),然後從右窗格中選擇“解除安裝”。
1.4 更新包
若要更新 NuGet 包,請執行以下步驟:
- 在“解決方案資料總管”中附加元件目,然後選擇“項目”>“管理 NuGet 包”。對于網站項目,請先選擇 Bin 檔案夾。
- 選擇“更新”頁籤以檢視具有所選包源中可用更新的包。選擇“包括預發行版”以在更新清單中包括預發行版包。
- 選擇要更新的包。在右側窗格中,從下拉清單中選擇所需的版本,然後選擇 更新。
1.5 管了解決方案的包
管了解決方案的包是同時處理多個項目的便捷方法:
- 在解決方案管理器中選擇一個解決方案,然後選擇“NuGet 包管理器>工具”>“管了解決方案的 NuGet 包”。
- 在“管了解決方案的 NuGet 包”視窗中,選擇受操作影響的項目。
1.6 “合并”頁籤
開發人員通常認為,在同一解決方案中的不同項目中跨不同項目使用同一 NuGet 包的不同版本是一種不好的做法。Visual Studio 允許你對 NuGet 包使用通用版本。為此,請使用“NuGet 包管理器”視窗的“合并”頁籤來發現解決方案中不同項目使用具有不同版本号的包的位置。
2、nuget配置程式源
2.1 線上源
如果遇到NuGet程式包加載逾時的問題時,可按如下步驟解決:
VS菜單: 工具 -> NuGet包管理器 -> 程式包管理器設定,如下所示:
我們知道,直接使用https://www.nuget.org/api/v2/源通路或下載下傳資源的速度在國内來說是非常慢的,我們可以添加國内的資源位址,如華為或騰訊等。
V3版本位址:https://api.nuget.org/v3/index.json
騰訊:https://mirrors.cloud.tencent.com/nuget/
部落格園:https://nuget.cnblogs.com/v3/index.json
華為雲: https://repo.huaweicloud.com/repository/nuget/v3/index.json
微軟Azure:https://nuget.cdn.azure.cn/v3/index.json
2.2 本地源
手動下載下傳nuget包,新增vs本地nuget下載下傳源。我們可以在Visual Studio中增加本地源:
在工具->選項->Nuget包管理器->程式包源中,可以增加本地的路徑。
在開發時,就可以從本地加載NuGet包了:
3、nuget安裝庫
3.1 nuget安裝nupengl.core(C++)
建立一個空項目工程如下:
打開nuget管理界面:
搜尋nupengl包:
選中第一個"nupengl.core",點選安裝。
相關包安裝之後,在項目檔案夾裡會自動産生一個檔案夾packages.
packages檔案夾裡存放本項目安裝的各個封包件。
這是nupengl包的庫檔案。
這是nupengl包的頭檔案。
在項目的解決方案的樹節點上也會自動增加一個節點“packages.config”
節點“packages.config”對應的檔案裡有本項目安裝的相關包的資訊。
3.2 nuget安裝Giawa.OpenGL(C#)
The Tao Framework+.net、SharpGL、opengl4csharp、OpenGL.Net、CSharpGL。
- 1、打開Visual Studio,建立個工程,winform或者控制台均可。
-
2、安裝Giawa.OpenGL和Giawa.OpenGL.Platform。
這2個庫來自于一個開源工程,叫opengl4csharp。
Github位址:https://github.com/giawa/opengl4csharp
在nuget裡面搜尋安裝即可。
- 3、下載下傳SDL2.dll并添加到bin目錄裡面。
4、代碼測試
給本測試項目添加一個cpp檔案:test.cpp
4.1 測試代碼:freeglut
test.cpp:
#include <GL/freeglut.h>
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(-0.5, -0.5, 0.0);
glColor3f(0, 1, 0);
glVertex3f(0.5, 0.0, 0.0);
glColor3f(0, 0, 1);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, (char**)argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 480);
glutCreateWindow("第一個glut程式 (XiaoMu 's Code)");
glutDisplayFunc(renderScene);
glutMainLoop();
return 0;
}
4.2 測試代碼:freeglut+tea
#include <gl/glut.h>
bool mouseLeftDown;
bool mouseRightDown;
GLfloat mouseX, mouseY;
GLfloat cameraDistance;
GLfloat cameraAngleX;
GLfloat cameraAngleY;
GLint winWidth = 600, winHeight = 600;
void RenderCB() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, cameraDistance);
glRotatef(cameraAngleX, 1, 0, 0);
glRotatef(cameraAngleY, 0, 1, 0);
glPushMatrix();
//glutWireTeapot(160);
glutSolidTeapot(120);
glPopMatrix();
glutSwapBuffers();
}
void ReshapeCB(int w, int h)
{
winWidth = w;
winHeight = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w / 2, w / 2, -h / 2, h / 2, -w, w);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void InitGL()
{
glClearColor(0.9, 0.9, 0.8, 1.0);
/********* 光照處理 **********/
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position0[] = { 100.0, 100.0, 100.0 ,0 };
//定義光位置得齊次坐标(x,y,z,w),
//如果w=1.0,為定位光源(也叫點光源),
//如果w=0,為定向光源(無限光源),定向光源為無窮遠點,因而産生光為平行光。
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); //環境光
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); //漫射光
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); //鏡面反射
glLightfv(GL_LIGHT0, GL_POSITION, light_position0); //光照位置
/******** 材質處理 ***********/
GLfloat mat_ambient[] = { 0.0, 0.2, 1.0, 1.0 };
GLfloat mat_diffuse[] = { 0.8, 0.5, 0.2, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 100.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void MouseCB(int button, int state, int x, int y)
{
mouseX = x;
mouseY = y;
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
{
mouseLeftDown = true;
}
else if (state == GLUT_UP)
mouseLeftDown = false;
}
else if (button == GLUT_RIGHT_BUTTON)
{
if (state == GLUT_DOWN)
{
mouseRightDown = true;
}
else if (state == GLUT_UP)
mouseRightDown = false;
}
}
void MouseMotionCB(int x, int y)
{
if (mouseLeftDown)
{
cameraAngleY += (x - mouseX);
cameraAngleX += (y - mouseY);
mouseX = x;
mouseY = y;
}
if (mouseRightDown)
{
cameraDistance += (y - mouseY) * 0.2f;
mouseY = y;
}
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutInitWindowPosition(300, 150);
glutCreateWindow("glut + tea (XiaoMu 's Code)");
glutDisplayFunc(RenderCB);
//glutPassiveMotionFunc(PassiveMouseMove);
glutMouseFunc(MouseCB);
glutMotionFunc(MouseMotionCB);
glutReshapeFunc(ReshapeCB);
InitGL();
glutMainLoop();
return 0;
}
4.3 測試代碼:freeglut+glew
#include <gl/glew.h>
#include <gl/freeglut.h>
#include <iostream>
using namespace std;
void changeViewport(int w, int h)
{
glViewport(0, 0, w, h);
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.8, 1, 0.8, 1);
glColor3f(1, 0, 1);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("glut + glew (XiaoMu 's Code)");
glutReshapeFunc(changeViewport);
glutDisplayFunc(render);
GLenum error = glewInit();
if (error != GLEW_OK)
{
printf("GLEW ERROR!");
return 1;
}
glutMainLoop();
return 0;
}
4.4 測試代碼:glfw
#include <iostream>
#include <GLFW/glfw3.h>
#pragma comment(lib, "opengl32.lib")
int main(void)
{
GLFWwindow *window;
/* Initialize the library */
if (!glfwInit())
return -1;
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//配置GLFW
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//配置GLFW
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//
// glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "glfw (XiaoMu 's Code)", nullptr, nullptr);
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);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
4.5 測試代碼:glfw+glew
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#pragma comment(lib, "opengl32.lib")
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, "glfw + glew (XiaoMu 's Code)", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (glewInit() == GLEW_OK)
{
std::cout << "GLEW IS OK\n";
}
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1, 1, 0, 1);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
4.6 測試代碼:glfw+glad
https://glad.dav1d.de/
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(800, 600, "glfw + glad (XiaoMu 's Code)", NULL, NULL);
if (window == NULL) {
cout << "Failed to create GLFW window" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 1, 1, 1);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
4.7 測試代碼:glfw+glad+shader
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec4 aColor;\n"
"out vec4 outColor;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
" outColor = aColor;\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec4 outColor;\n"
"void main()\n"
"{\n"
" //FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
" FragColor = outColor;\n"
"}\n\0";
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow * window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "glfw + glad + shader (XiaoMu 's Code)", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// vertex shader
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
float vertices[] = {
-0.9f, -0.9f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.9f, 0.9f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.8f, -0.8f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
};
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_LINES, 0, 4);
// glBindVertexArray(0); // no need to unbind it every time
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
glfwTerminate();
return 0;
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow * window, int width, int height)
{
glViewport(0, 0, width, height);
}
4.8 測試代碼:sdl2
在nuget的包管理器界面搜尋sdl2.nuget的記錄。
點選右側的安裝按鈕。
包下載下傳完成之後,自動彈出如下界面提示将sdl2包的相關資料內建到目前項目工程中。
在packages.config檔案發現增加了sdl相關包。
編寫如下測試代碼:
- test.cpp:
#include <iostream>
#include <SDL.h>
bool m_running = true;
int main(int argc, char** argv) {
// 初始化video子系統
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL INIT FAILED." << std::endl;
return -1;
}
auto* window = SDL_CreateWindow("SDL2 (XiaoMu 's Code)",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
800, 600,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
// 用來接收事件的結構體
SDL_Event e;
while (m_running) {
// 事件處理
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
m_running = false;
}
}
}
SDL_DestroyWindow(window);
return 0;
}
4.9 測試代碼:sdl2+glew
編寫如下測試代碼:
- test.cpp:
// OpenGL headers
//#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glu.h>
#include <GL/gl.h>
// SDL headers
#include <SDL_main.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <iostream>
#pragma comment(lib, "opengl32.lib")
bool quit;
SDL_Window* window;
SDL_GLContext glContext;
SDL_Event sdlEvent;
int main(int argc, char *argv[])
{
quit = false;
//Use OpenGL 3.1 core
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// Initialize video subsystem
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
// Display error message
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
}
else
{
// Create window
window = SDL_CreateWindow("SDL2 + glew (XiaoMu 's Code)", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (window == NULL)
{
// Display error message
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return false;
}
else
{
// Create OpenGL context
glContext = SDL_GL_CreateContext(window);
if (glContext == NULL)
{
// Display error message
printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
return false;
}
else
{
// Initialize glew
glewInit();
}
}
}
// Game loop
while (!quit)
{
while (SDL_PollEvent(&sdlEvent) != 0)
{
// Esc button is pressed
if (sdlEvent.type == SDL_QUIT)
{
quit = true;
}
}
// Set background color as cornflower blue
glClearColor(0.39f, 0.58f, 0.93f, 1.f);
// Clear color buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(-0.5, -0.5, 0.0);
glColor3f(1, 0, 1);
glVertex3f(0.5, 0.0, 0.0);
glColor3f(1, 1, 0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
// Update window with OpenGL rendering
SDL_GL_SwapWindow(window);
}
//Destroy window
SDL_DestroyWindow(window);
window = NULL;
//Quit SDL subsystems
SDL_Quit();
return 0;
}
4.10 測試代碼:sdl2+glad
編寫如下測試代碼:
- test.cpp:
#include <iostream>
#include <glad/glad.h>
#include <SDL.h>
bool m_running = true;
int main(int argc, char** argv) {
// 初始化video子系統
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL INIT FAILED." << std::endl;
return -1;
}
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
auto* window = SDL_CreateWindow("SDL2 + glad (XiaoMu 's Code)",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
800, 600,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
auto context = SDL_GL_CreateContext(window);
gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress);
// 用來接收事件的結構體
SDL_Event e;
while (m_running) {
glClearColor(0.39f, 0.58f, 0.93f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
// 事件處理
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
m_running = false;
}
}
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(-0.5, -0.5, 0.0);
glColor3f(0, 1, 0);
glVertex3f(0.8, 0.8, 0.0);
glColor3f(0, 0, 1);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
return 0;
}