WhyCan Forum(哇酷论坛)

人过留名,雁过留声,感谢各位朋友不离不弃地支持。 QQ: 516333132, 微信: whycan_cn (哇酷网/挖坑网/填坑网) admin@whycan.cn

您尚未登录。

#1 2020-01-07 10:14:41

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

^_^

离线

#2 2020-01-07 10:18:13

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

2020-01-07_101534.png

离线

#3 2020-01-07 10:19:30

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

2020-01-07_101705.png

nuget 安装 gflw 包, 否则需要去这里下载 https://www.glfw.org/download.html

下载预编译的win32 sdk 包, 添加头文件和库文件, 手续繁琐.

离线

#4 2020-01-07 10:21:29

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

打开 glfw 官网: https://www.glfw.org/docs/3.1/quick.html

找到那个三角形 demo 程序

#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
static void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
    GLFWwindow* window;
    glfwSetErrorCallback(error_callback);
    if (!glfwInit())
        exit(EXIT_FAILURE);
    window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);
    glfwSetKeyCallback(window, key_callback);
    while (!glfwWindowShouldClose(window))
    {
        float ratio;
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        ratio = width / (float) height;
        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
        glBegin(GL_TRIANGLES);
        glColor3f(1.f, 0.f, 0.f);
        glVertex3f(-0.6f, -0.4f, 0.f);
        glColor3f(0.f, 1.f, 0.f);
        glVertex3f(0.6f, -0.4f, 0.f);
        glColor3f(0.f, 0.f, 1.f);
        glVertex3f(0.f, 0.6f, 0.f);
        glEnd();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}

有链接错误, 所以中间添加两行:

#pragma comment(lib, "glfw3dll.lib")
#pragma comment(lib, "OpenGL32.Lib")

离线

#5 2020-01-07 10:21:59

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

2020-01-07_101824.png

运行完全 OK

离线

#6 2020-01-07 10:30:47

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

2020-01-07_103011.png


也可以使用静态链接:

#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "OpenGL32.Lib")

离线

#7 2020-01-07 10:38:45

smartcar
会员
注册时间: 2018-02-19
累计积分: 465

Re: MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

这么吊,我也学学。

离线

#8 2020-01-07 10:52:07

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

smartcar 说:

这么吊,我也学学。

互相学习!



2020-01-07_105005.png

#include <SDL.h>
#include <SDL_opengl.h>

#include <stdlib.h> //rand()

#pragma comment(lib, "OpenGL32.Lib")
#pragma comment(lib, "SDL2main.lib")

#undef main

//SDL2 flashing random color example
//Should work on iOS/Android/Mac/Windows/Linux

static bool quitting = false;
static float r = 0.0f;
static SDL_Window *window = NULL;
static SDL_GLContext gl_context;


void render() {

	SDL_GL_MakeCurrent(window, gl_context);

	r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);

	glClearColor(r, 0.4f, 0.1f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	SDL_GL_SwapWindow(window);

} //render


int SDLCALL watch(void *userdata, SDL_Event* event) {

	if (event->type == SDL_APP_WILLENTERBACKGROUND) {
		quitting = true;
	}

	return 1;
}

int main(int argc, char *argv[]) {


	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
		SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
		return 1;
	}

	window = SDL_CreateWindow("title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 512, 512, SDL_WINDOW_OPENGL);

	gl_context = SDL_GL_CreateContext(window);

	SDL_AddEventWatch(watch, NULL);


	while (!quitting) {

		SDL_Event event;
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT) {
				quitting = true;
			}
		}

		render();
		SDL_Delay(2);

	}

	SDL_DelEventWatch(watch, NULL);
	SDL_GL_DeleteContext(gl_context);
	SDL_DestroyWindow(window);
	SDL_Quit();

	exit(0);

} //main

https://gist.github.com/underscorediscovery/46e4f5b4e3e6de7ad50d

2020-01-07_105209.png

三下五除二搞定了sdl2 demo.

离线

#9 2020-01-07 11:16:20

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

好吧, 源码上交: nuget_demo_glfw_sdl2.7z

离线

#10 2020-01-07 11:17:02

win32prog
会员
注册时间: 2019-11-28
累计积分: 138

Re: MSVC的 NuGet 包管理器真香!首先跑个 opengl 测试程序试一试

编译 win32/x64 都很方便,不需要自己手动设置什么。

离线

页脚