回滚代码从头读发现不初始化为0,就会错位闪退问题,其实就是默认拖拽了一次,拖拽方向未知
draftflag = 0;
#include <raylib.h>
#include <stdio.h>
int main() {
const int screenWidth = 750;
const int screenHeight = 750;
//拖拽
int draftflag;
int oldx;
int oldy;
int draftx;
int drafty;
int gamex;
int gamey;
// 不初始化为0,就会错位闪退
draftflag = 0;
oldx = 0;
oldy = 0;
draftx = 0;
drafty = 0;
gamex = 0;
gamey = 0;
// 绘制
int drawi;
int drawj;
int drawx;
int drawy;
drawi = 0;
drawj = 0;
drawx = 0;
drawy = 0;
int pixnum;
int** map;
map = new int*[750 / 30];
for (int i = 0; i < 25; i++) {
map[i] = new int[1500 / 30];
}
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 50; j++) {
map[i][j] = 0;
}
}
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 50; j++) {
printf("%d ", map[i][j]);
}
printf("\n");
};
InitWindow(screenWidth, screenHeight, "test");
// 设置帧率
SetTargetFPS(150);
// 设置一个画布,可以使用GPU进行绘制
// RenderTexture mesh = LoadRenderTexture(17000, 17000);
RenderTexture mesh = LoadRenderTexture(750, 750);
// RenderTexture mesh = LoadRenderTexture(16000, 16000);
// 17000*17000不行
// 16000*16000可行
BeginTextureMode(mesh);
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 50; j++) {
if (map[i][j] == 0) {
DrawRectangle(j * 30, i * 30, 30, 30, {0, 255, 0, 255});
}
}
}
EndTextureMode();
BeginDrawing();
// 绘制背景,用黑色涂满屏幕
ClearBackground(BLACK);
// 开始粘贴
DrawTexturePro(
// 选择要粘贴的画布
mesh.texture
// 采样的矩形区域
, { 0 - draftx, 0 - drafty, 750.0f, 750.0f }
// 实际绘制的矩形区域,如果和采样区不相同,就会自动放缩
, {100, 100, 750, 750}
// 绘制的左上角坐标,改参数可知绘制到屏幕时,x正方向向左,y正方向向上
, {0, 0}
, 0, WHITE);
// 绘制帧率,显示每秒刷新多少次
DrawText(TextFormat("%i FPS", GetFPS()), 2 + 10, 2 + 10, 20, WHITE);
DrawText(TextFormat("%f,%f", GetMousePosition().x, GetMousePosition().y), 35, 12, 30, WHITE);
// 打印鼠标滑轮滚动
DrawText(TextFormat("%f", GetMouseWheelMove()), 12, 52, 20, WHITE);
if (draftflag) {
DrawText(TextFormat("Flaging"), 12, 92, 20, WHITE);
}
EndDrawing();
int a = 0;
int have = 0;
while (!WindowShouldClose()) {
have++;
if (have > 150) {
a++;
have = 0;
}
if (a > 100) {
break;
}
}
while (!WindowShouldClose()) {
//鼠标长按检测,右键拖拽
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
draftflag = 1;
oldx = GetMousePosition().x;
oldy = GetMousePosition().y;
}
if (draftflag) {
draftx = gamex - (GetMousePosition().x - oldx) ;
drafty = gamey - (GetMousePosition().y - oldy) ;
}
// 鼠标松开,记录本次拖拽的结束位置
if (IsMouseButtonUp(MOUSE_BUTTON_RIGHT)) {
draftflag = 0;
oldx = 0;
oldy = 0;
gamex = draftx;
gamey = drafty;
}
// 设置默认某个要绘制的画布,以后的绘制都会画到这个默认画布上
BeginTextureMode(mesh);
ClearBackground(WHITE);
// 如果鼠标左键按下去了,就一直绘制
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
// 在鼠标点击的位置绘制矩形,鼠标坐标是x的正方向是往右,y的正方向是往下
drawy = GetMousePosition().y - 0;
drawx = GetMousePosition().x - 0;
drawi = (drawy + drafty) / 30;
drawj = (drawx + draftx) / 30;
// 注意鼠标在屏幕的坐标和GPU坐标区别,GPU坐标x正方向向右
map[drawi + 1][drawj] = 1;
DrawRectangle(drawj * 30, 750 - drawi * 30, 30, 30, GREEN);
}
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 50; j++) {
if (map[i][j] == 1) {
DrawRectangle(j * 30, 750 - i * 30, 30, 30, {255, 0, 0, 255});
} else {
DrawRectangle(j * 30, 750 - i * 30, 30, 30, {0, 255, 0, 255});
}
if (i == 0 || j == 0 || j == 24 || i == 24) {
DrawRectangle(j * 30, 750 - i * 30, 30, 30, {0, 255, 255, 255});
}
}
}
// 画一个黄色矩形,改参数可知GPU绘制缓存画布时,x正方向向右,y正方向向上,左下角是0,0原点
// DrawRectangle(0, 0, 400, 200, YELLOW);
// 结束默认设置绘制区域
EndTextureMode();
// 开始把画布数据绘制到屏幕上,或者说设置默认画布是屏幕
BeginDrawing();
// 绘制背景,用黑色涂满屏幕
ClearBackground(BLACK);
// 开始粘贴
DrawTexturePro(
// 选择要粘贴的画布
mesh.texture
// 采样的矩形区域
, { 0 + draftx, 0 + drafty, 750.0f, 750.0f }
// 实际绘制的矩形区域,如果和采样区不相同,就会自动放缩
, {0, 0, 750, 750}
// 绘制的左上角坐标,改参数可知绘制到屏幕时,x正方向向左,y正方向向上
, {0, 0}
, 0, WHITE);
// 绘制帧率,显示每秒刷新多少次
DrawText(TextFormat("%d,%d Mouse", drawj, drawi + 1), 2 + 10, 2 + 90, 20, BLUE);
DrawText(TextFormat("%d,%d Mouse", drawx, drawy ), 560 + 10, 2 + 130, 20, BLUE);
DrawText(TextFormat("%d,%d Mousedraft", draftx, drafty ), 560 + 10, 2 + 230, 20, BLUE);
DrawText(TextFormat("%i FPS", GetFPS()), 2 + 10, 2 + 10, 20, WHITE);
DrawText(TextFormat("%f,%f", GetMousePosition().x, GetMousePosition().y), 150, 12, 30, BLACK);
// 打印鼠标滑轮滚动
DrawText(TextFormat("%f", GetMouseWheelMove()), 12, 52, 20, BLACK);
DrawText(TextFormat("%d", map[drawj][drawi + 1]), 372, 52, 20, BLACK);
if (draftflag) {
DrawText(TextFormat("Flaging"), 12, 92, 20, BLACK);
}
EndDrawing();
}
}