Raylib 坐标系

draftx 符号调整为正数

发现采样坐标系原点0,0 在左上角,正方向 右,下

绘制坐标系 原点0,0 在左下角,正方向 右,上

拖拽可得

 

 


#include <raylib.h>
// 重整原因:解决新函数放大缩小之下,raylib 的网格采样部分,选择数组的一部分刷新倒缓冲区里
// 从直接建立缓冲区,到先在数组里进行移动,然后再设置检查缓冲区


int main() {
	int **map;
	map = new int*[250];
	for (int i = 0; i < 250; i++) {
		map[i] = new int[250];
	}
	for (int i = 0; i < 250; i++) {
		for (int j = 0; j < 250; j++) {
//			测试数据,渐变 25改105 柔和
			map[i][j] = (j + i) % 105;
		}
	}

//	初始化窗口
	InitWindow(1750, 1050, "test for location");
//	设置GPU可以操作的画布,一定要再初始化窗口之后才行,实际上是OpenGL的上下文,或者说默认环境设置
	RenderTexture2D mesh = LoadRenderTexture(750 + 30 * 2, 750 + 30 * 2);
//	设置帧率
	SetTargetFPS(160);
//		设置默认绘制到mesh
	BeginTextureMode(mesh);
	for (int i = 0; i < 25; i++) {
		for (int j = 0; j < 25; j++) {
//				绘制矩形,原点(0,0)在左下角,现在是从左下角一行一行往上绘制
			if (i == 0 || j == 0 || i == 24 || j == 24) {
				DrawRectangle(j * 30, i * 30, 30, 30, {0, 255, 255, 255});
			} else  {
				DrawRectangle(j * 30, i * 30, 30, 30, {map[i][j] * 5 % 255, map[i][j] * 5 % 255, 255, 255});
			}
		}
	}
//		取消绘制的GPU画布
	EndTextureMode();
//		设置默认绘制到桌面
	BeginDrawing();
//		黑色覆盖全部屏幕
	ClearBackground(BLACK);
	DrawTexturePro(mesh.texture, {0, 0, 750, 750}, {0, 0, 750, 750}, {0, 0}, 0, WHITE);
//		结束绘制的桌面
	EndDrawing();

	int mousex;
	int mousey;
	mousex = 0;
	mousey = 0;
	float camerasize;
	camerasize = 1;
//	拖拽
	int draftflag;
	int draftx;
	int drafty;
	int gamex;
	int gamey;
//	记录长按时,鼠标按下去的位置,减出长按拖拽距离
	int oldx;
	int oldy;
	draftflag = 0;
	draftx = 0;
	drafty = 0;
	gamex = 0;
	gamey = 0;

//	记录bkmeshmap 网格,用于出界刷新
	int bkmeshmapi;
	int bkmeshmapj;
	int bkmeshmapmaxi;
	int bkmeshmapmaxj;
	bkmeshmapi = 0;
	bkmeshmapi = 0;
	bkmeshmapmaxi = 25 + 2;
	bkmeshmapmaxj = 25 + 2;

	//	拖拽边界
	int limitright;
	int limitleft;
	int limittop;
	int limitbottom;

//	坐标系变化,现在是在底部进行,左下角是0,0原点
	limitright = 25 * 30 + 30 * 2;
	limitleft = 0;
	limittop = 25 * 30 + 30 * 2;
	limitbottom = 0;


//	这里开始主循环
	while (!WindowShouldClose()) {
//		注意是pressed 不是 Down
		if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
			draftflag = 1;
			oldx = GetMouseX();
			oldy = GetMouseY();
		}
		if (draftflag == 1) {
			mousex = GetMouseX();
			mousey = GetMouseY();
			draftx = gamex - (mousex - oldx) / camerasize;
			drafty = gamey - (mousey - oldy) / camerasize;
		}
		if (IsMouseButtonUp(MOUSE_BUTTON_RIGHT)) {
			draftflag = 0;
			oldx = 0;
			oldy = 0;
			gamex = draftx;
			gamey = drafty;
		}


		if (draftx < limitleft && draftx > 30) {
			bkmeshmapj - 1;
			limitleft -= 30;
		} else if (draftx > limitright + bkmeshmapi * 30 && draftx < 250 * 30 - 750 - 30) {
			bkmeshmapj + 1;
			limitright += 30;
		}

		if (drafty < limitbottom && drafty > 30) {
			bkmeshmapj - 1;
			limittop -= 30;
		} else if (draftx > limittop && drafty < 250 * 30 - 750 - 30) {
			bkmeshmapj + 1;
			limitbottom += 30;
		}


//		设置默认绘制到mesh
		BeginTextureMode(mesh);
		for (int i = 0; i < 25 + 2; i++) {
			for (int j = 0; j < 25 + 2; j++) {
//				绘制矩形,原点(0,0)在左下角,现在是从左下角一行一行往上绘制
				if (map[i][j] == 266) {
					DrawRectangle(j * 30, i * 30, 30, 30, {255, 0, 255, 255});
				} else {
					DrawRectangle(j * 30, i * 30, 30, 30, {map[i + bkmeshmapi][j + bkmeshmapj] * 5 % 255, map[i + bkmeshmapi][j + bkmeshmapj] * 5 % 255, 255, 255});
				}
				DrawRectangle(0, 0, 50, 50, {154, 154, 154, 255});
//				绘制坐标系是左下角0,0)y正方向向上
			}
		}
//		取消绘制的GPU画布
		EndTextureMode();
//		设置默认绘制到桌面
		BeginDrawing();
//		黑色覆盖全部屏幕
		ClearBackground(BLACK);
//		采样坐标系是左上角0,0,y轴正方向向下
		DrawTexturePro(mesh.texture, {draftx - bkmeshmapi * 30, drafty - bkmeshmapj * 30, 750 / camerasize, 750 / camerasize}, {0, 0, 750, 750}, {0, 0}, 0, WHITE);

		DrawText(TextFormat("mouseV1 %.0f,%.0f", GetMousePosition().x, GetMousePosition().y), 35, 12, 30, BLUE);
		DrawText(TextFormat("mouseV2 %.0f,%.0f", GetMousePosition().x, 750 - GetMousePosition().y), 35, 62, 30, BLUE);

		DrawText(TextFormat("draftxyV2 %d,%d", draftx, drafty), 35, 152, 30, RED);
		DrawText(TextFormat("camerasize %f", camerasize), 35, 192, 30, BLACK);
//		结束绘制的桌面
		EndDrawing();
	}
}

上一篇:C语言编程与进阶