vscode+imgui+sdlrenderer2,mingw跨平台编译

找了一圈发现都是opengl,dx的。sdlrenderer的一个没有。这个renderer才是跨平台的啊,渲染封装到libsdl了,有没有gpu都能干活。

准备imgui和sdl

一。去www.libsdl.org左下角sdl releases列表找到最新版本

window下用SDL2-devel-2.30.1-mingw.zip,因为vc跨平台不优雅。

解压后有i686-w64-mingw32和x86_64-w64-mingw32二种cpu版本,我用w64

window下不用编译,官方提供了sdl2.dll,但ubuntu下官方没有提供,所以得自己编。

下载sdl代码SDL2-2.30.1.tar.gz解压到ubuntu上去。

解压后有INSTALL.txt,里边有说明

        Linux and other UNIX systems:
        * Run './configure; make; make install'

改权限,照着做,最后编译完成得到

这个libSDL2.a是个静链库,什么x11,Xcursor什么都被包含在里边

到此window下的libSDL2.dll和ubuntu下的libSDL2.a都有了,接下来搞imgui。

imgui使用sdlrenderer2做后台,测试代码还是用官方main

目录结构如下

Cmakelists如下

window下vscode记得选mingw进行编译

代码整个复制到ubuntu

window下运行

ubuntu下运行

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用VS Code和C++编写贪吃蛇的步骤: 1. 安装VS Code和MinGW - 下载并安装VS Code:https://code.visualstudio.com/ - 下载并安装MinGW:http://www.mingw.org/ 2. 配置环境变量 - 将MinGW的bin目录添加到系统环境变量中 3. 创建C++项目 - 在VS Code中创建一个新的文件夹,作为项目的根目录 - 在VS Code中打开该文件夹 - 在VS Code的终端中输入以下命令,创建一个名为main.cpp的文件: ``` touch main.cpp ``` - 在main.cpp中编写贪吃蛇的代码 4. 编写贪吃蛇代码 - 可以使用C++的图形库,如graphics.h - 也可以使用第三方图形库,如SDL、SFML等 5. 编译和运行代码 - 在VS Code的终端中输入以下命令,编译代码: ``` g++ main.cpp -o main ``` - 在VS Code的终端中输入以下命令,运行代码: ``` ./main ``` 以下是一个简单的C++贪吃蛇代码示例: ```c++ #include <iostream> #include <conio.h> #include <windows.h> using namespace std; bool gameOver; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; print = true; } } if (!print) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Score:" << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x > width || x < 0 || y > height || y < 0) gameOver = true; for (int i = 0; i < nTail; i++) if (tailX[i] == x && tailY[i] == y) gameOver = true; if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(50); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值