bitmap.cpp

来自「清华关于C++ 的程序讲义 值得一看 关于算法」· C++ 代码 · 共 60 行

CPP
60
字号
// demonstrate selecting an action by clicking in a bitmap

#include <assert.h>
#include <bitmap.h>

// define a window
SimpleWindow FlipWindow("FlipCard", 15.0, 9.0, Position(1.0, 1.0));

// define bitmaps for the front and back of a card
BitMap CardFront(FlipWindow);
BitMap CardBack(FlipWindow);

// need a type and object for remembering which side
// of the card is showing
enum Side { Front, Back };
Side SideShowing;

// MouseClickEvent(): come here when user clicks mouse
int MouseClickEvent(const Position &MousePosition) {
	if (CardFront.IsInside(MousePosition)) {
		// card is selected so flip it
		if (SideShowing == Back) {
			SideShowing = Front;
			CardFront.Draw();
		}
		else if (SideShowing == Front) {
			SideShowing = Back;
			CardBack.Draw();
		}
	}
	return 1;
}

int ApiMain() {
	// open the window
	FlipWindow.Open();
	assert(FlipWindow.GetStatus() == WindowOpen);
	
	// Load the images
	CardFront.Load("c1.bmp");
	assert(CardFront.GetStatus() == BitMapOkay);
	
	CardBack.Load("cardbk1.bmp");
	assert(CardBack.GetStatus() == BitMapOkay);
	
	// compute position to display the card
	Position CardPosition = FlipWindow.GetCenter() +
	    Position(-.5*CardFront.GetWidth(), -.5*CardFront.GetHeight());
	
	CardFront.SetPosition(CardPosition);
	CardBack.SetPosition(CardPosition);
	
	SideShowing = Front;
	CardFront.Draw();
	
	// set up mouse callback
	FlipWindow.SetMouseClickCallback(MouseClickEvent);
	
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?