• キーボード入力
  • マウス入力
  • マウスカーソル位置
  • 文字列描画関数
  • 図形描画関数
  • ダブルバッファ

マウスを押した位置にサークルを書きたい
けれども、プログラムの流れ的に

  1. バックグラウンドクリア
  2. バックバッファに描画
  3. 画面フリップ

を繰り返しているので、マウスクリックした場所が毎回更新されて、前のクリック位置は消えてしまう。
お絵かきソフトみたいに、マウスを押しながら動かした軌跡を描いてみたいなぁって欲望を満たす。

Listing. 1: DxLib基本編
#include <dxlib.h>
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{
	const int WIDTH = 960; // Width of the window	
	const int HEIGHT = 640; // Height of the window
	const int WHITE = GetColor(255, 255, 255); // Color white
 
 
	SetWindowText("DXLib Window"); // Set the window title
	SetGraphMode(WIDTH, HEIGHT, 32); // Set the graphics mode	
	ChangeWindowMode(TRUE); // Change to windowed mode
 
	// Initialize DXLib
	if (DxLib_Init() == -1) {
		return -1; // Initialization failed
	}
 
	SetBackgroundColor(72, 61, 139); // Set the background color to white
	SetDrawScreen(DX_SCREEN_BACK); // Set the drawing screen to back buffer
	int timer = 0;
	int colors[7] = {
		GetColor(255, 0, 0),   // Red
		GetColor(0, 255, 0),   // Green
		GetColor(0, 0, 255),   // Blue
		GetColor(255, 255, 0), // Yellow
		GetColor(255, 165, 0), // Orange
		GetColor(75, 0, 130),   // Indigo
		GetColor(238, 130, 238) // Violet
	};
	long long int inputNum = 0; // Index for cycling through colors
	const int MAX_MOUSE_POS = 10000; // Maximum mouse position array size
 
	struct Point //座標を保持する構造体
	{
		int x = -1;
		int y = -1;
	};
 
	Point mPos[MAX_MOUSE_POS] = { {-1, -1} }; // Array to hold mouse positions
 
	//mPos[0].x
	//mPos[0].y
	//
	//	mPos[1]
	//	・・・
	//mPos[MAX_MOUSE_POS - 1]
	// Main loop
	while (1) {
		ClearDrawScreen(); // Clear the screen
		timer++;
		DrawFormatString(0, 0, WHITE, "Timer: %d", timer); // Draw the timer value
 
		//if (CheckHitKey(KEY_INPUT_UP)) DrawString(0, 20, "Up key pressed", WHITE); // Draw if the Up key is pressed
		//if (CheckHitKey(KEY_INPUT_DOWN)) DrawString(0, 40, "Down key pressed", WHITE); // Draw if the Down key is pressed
		//if (CheckHitKey(KEY_INPUT_LEFT)) DrawString(0, 60, "Left key pressed", WHITE); // Draw if the Left key is pressed
		//if (CheckHitKey(KEY_INPUT_RIGHT)) DrawString(0, 80, "Right key pressed", WHITE); // Draw if the Right key is pressed
		//DrawBox(100, 100, 300, 300, GetColor(255, 0, 0), TRUE); // Draw a filled red box
		//DrawBox(100, 100, 300, 300, GetColor(0, 255, 0), FALSE, 3);
		int mouseX, mouseY;
		GetMousePoint(&mouseX, &mouseY); // Get mouse input
		DrawFormatString(400, 0, WHITE, "Mouse Position: (%d, %d)", mouseX, mouseY); // Draw the mouse position
		if (GetMouseInput() & MOUSE_INPUT_LEFT) {
			DrawString(400, 20, "Left mouse button pressed", WHITE); // Draw if the left mouse button is pressed
 
			mPos[inputNum % MAX_MOUSE_POS].x = mouseX; // Store the mouse X position
			mPos[inputNum % MAX_MOUSE_POS].y = mouseY; // Store the mouse Y position
 
			inputNum++; // Increment the input number to cycle through colors
		}
 
 
		for (int i = 0;i < MAX_MOUSE_POS;i++) {
			if (mPos[i].x != -1 && mPos[i].y != -1) {
				DrawCircle(mPos[i].x, mPos[i].y, 20, colors[i%7], TRUE); // Draw circles at stored mouse positions
			}
		}
		if (GetMouseInput() & MOUSE_INPUT_RIGHT) DrawString(400, 40, "Right mouse button pressed", WHITE); // Draw if the right mouse button is pressed
 
		// Your drawing code goes here
		ScreenFlip(); // Update the screen
		WaitTimer(33); // Wait for approximately 33 milliseconds (30 FPS)
		if (ProcessMessage() == -1) {
			break; // Exit the loop if a message indicates to quit
		}
		if (CheckHitKey(KEY_INPUT_ESCAPE)) {
			break; // Exit the loop if the Escape key is pressed
		}
	}
	// Terminate DXLib
	DxLib_End();
	return 0;
}
  • game-engineer/classes/2025/game-programing-basic/first-term/06-23-0405.txt
  • 最終更新: 8カ月前
  • by root