📜  C程序使用图形显示带有消息的动画鞭炮(1)

📅  最后修改于: 2023-12-03 15:14:26.135000             🧑  作者: Mango

C程序使用图形显示带有消息的动画鞭炮

本程序演示了如何使用C语言创建一个基于图形库的动画鞭炮,并在鞭炮爆炸时显示一条消息。以下是程序的演示效果:

动画鞭炮演示效果

开发环境

本程序使用以下开发环境:

  • 操作系统:macOS
  • 编辑器:Visual Studio Code
  • 编译器:GCC
  • 图形库:OpenGL

在编写本程序时,我们使用了 OpenGL 这个图形库来实现动画效果。如果你还没有安装 OpenGL,可以参考以下步骤:

  1. 安装 Xcode 开发工具:

    $ xcode-select --install
    
  2. 安装 Homebrew 包管理器:

    $ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  3. 安装 GLFW 和 GLEW 库:

    $ brew install glfw
    $ brew install glew
    
程序代码

以下是本程序的核心代码,你可以将其保存为一个 .c 文件,并使用 GCC 编译器编译运行:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define PI 3.141592654

// 渲染帧回调函数
void renderFrame(GLFWwindow* window) {
  double time = glfwGetTime();

  // 清除画布
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT);

  // 画出烟花效果
  double cx = WINDOW_WIDTH / 2;
  double cy = WINDOW_HEIGHT / 2;
  double r = 100;
  double dt = 0.1;
  double t = time / 3.0;
  double x = cx + r * cos(t);
  double y = cy + r * sin(t);
  double r2 = 20;
  double dt2 = 0.02;
  double t2 = time * 10;
  double x2 = x + r2 * cos(t2);
  double y2 = y + r2 * sin(t2);
  double r3 = 10;
  double dt3 = 0.03;
  double t3 = time * 20;
  double x3 = x2 + r3 * cos(t3);
  double y3 = y2 + r3 * sin(t3);

  glBegin(GL_TRIANGLES);
  glColor3f(1.0, 0.0, 0.0);
  glVertex2f(x, y);
  glColor3f(0.0, 1.0, 0.0);
  glVertex2f(x2, y2);
  glColor3f(0.0, 0.0, 1.0);
  glVertex2f(x3, y3);
  glEnd();

  // 在鞭炮爆炸时显示消息
  if (x3 < 10 || x3 > WINDOW_WIDTH - 10 || y3 < 10 || y3 > WINDOW_HEIGHT - 10) {
    printf("Happy New Year!\n");
  }
}

int main() {
  // 初始化 GLFW 库
  if (!glfwInit()) {
    fprintf(stderr, "Failed to initialize GLFW.\n");
    return -1;
  }

  // 创建窗口
  GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Fireworks", NULL, NULL);
  if (!window) {
    fprintf(stderr, "Failed to create GLFW window.\n");
    glfwTerminate();
    return -1;
  }

  // 设置当前窗口为上下文
  glfwMakeContextCurrent(window);

  // 初始化 GLEW 库
  if (glewInit() != GLEW_OK) {
    fprintf(stderr, "Failed to initialize GLEW.\n");
    glfwTerminate();
    return -1;
  }

  // 设置渲染帧回调函数
  glfwSetFramebufferSizeCallback(window, renderFrame);

  // 循环渲染帧
  while (!glfwWindowShouldClose(window)) {
    renderFrame(window);
    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  // 清除 GLFW 和 GLEW 库
  glfwDestroyWindow(window);
  glfwTerminate();

  return 0;
}
解析

以下是程序中重要的部分的解析:

// 渲染帧回调函数
void renderFrame(GLFWwindow* window) {
  double time = glfwGetTime();

  // 清除画布
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT);

  // 画出烟花效果
  // ...

renderFrame 函数是该程序的渲染帧回调函数,它使用当前时间 time 来计算烟花的位置,并通过 OpenGL 函数画出一个三角形表示烟花。

// 在鞭炮爆炸时显示消息
if (x3 < 10 || x3 > WINDOW_WIDTH - 10 || y3 < 10 || y3 > WINDOW_HEIGHT - 10) {
  printf("Happy New Year!\n");
}

如果当前的烟花在窗口的边缘,那么就会输出一条 "Happy New Year!" 的消息。

// 创建窗口
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Fireworks", NULL, NULL);
if (!window) {
  fprintf(stderr, "Failed to create GLFW window.\n");
  glfwTerminate();
  return -1;
}

// 设置当前窗口为上下文
glfwMakeContextCurrent(window);

这些代码会创建一个名为 "Fireworks" 的窗口,将其设置为当前上下文,并在下面的循环中不停地渲染帧。

// 循环渲染帧
while (!glfwWindowShouldClose(window)) {
  renderFrame(window);
  glfwSwapBuffers(window);
  glfwPollEvents();
}

通过不断调用 renderFrame 函数,以及交换缓冲区和轮询事件等操作,循环渲染帧,使整个动画看起来是连续的。

结语

本程序演示了如何使用 C 语言和 OpenGL 图形库创建动画,以及在鞭炮爆炸时显示一条消息的方法。如果你想深入了解 OpenGL,可以参考 OpenGL 的官方文档 OpenGL - The Industry Standard for High Performance Graphics