^_^
离线
离线
离线
打开 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")
离线
运行完全 OK
离线
也可以使用静态链接:
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "OpenGL32.Lib")
离线
这么吊,我也学学。
离线
这么吊,我也学学。
互相学习!
#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
三下五除二搞定了sdl2 demo.
离线
好吧, 源码上交: nuget_demo_glfw_sdl2.7z
离线
编译 win32/x64 都很方便,不需要自己手动设置什么。
离线