main.cpp
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include "dispix.h"
#define FPS_SET 60 //帧率
#define WINDOW_HEIGHT 240
#define WINDOW_WIDTH 320
HWND hwnd_main; //主窗口标识
CDisPix disp(WINDOW_HEIGHT, WINDOW_WIDTH);
int y = 20;
int x = 20;
BOOL InitWindowsClass(HINSTANCE hInstance, const char *name); //注册窗口
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow, const char *name, const char *name_show); //创建窗口
VOID CALLBACK LocalTimerProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime); //定时器中断,用来刷屏
long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam); //消息处理
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Message;
if (!InitWindowsClass(hInstance, "hellow")) return FALSE;
if (!InitWindows(hInstance, nCmdShow, "hellow", "hellow windows")) return FALSE;
if (!SetTimer(NULL, 1, 1000 / FPS_SET, LocalTimerProc)) return FALSE;
while (GetMessage(&Message, 0, 0, 0))//进入消息循环
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
//===================================消息处理===================================
long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam)
{
HDC hDC;
PAINTSTRUCT PtStr;
switch (iMessage)
{
case WM_PAINT://重绘
hDC = BeginPaint(hWnd, &PtStr);
disp.SetHollowRect(y, x, 0, 0, 255, 20, 20);
disp.UpdatePixel(hDC);
EndPaint(hWnd, &PtStr);
return 0;
case WM_CHAR://按键信息
disp.SetHollowRect(y, x, 255, 0, 0, 20, 20);
switch (wParam)
{
case'w':
y -= 1;
break;
case's':
y += 1;
break;
case'a':
x -= 1;
break;
case'd':
x += 1;
break;
default:
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hWnd, iMessage, wParam, lParam));
}
}
//==================================================================================
BOOL InitWindowsClass(HINSTANCE hInstance, const char *name)
{
WNDCLASS WndClass;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = NULL;
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, "END");
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc; //消息处理函数
WndClass.lpszClassName = LPCSTR(name);
WndClass.lpszMenuName = NULL;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
return RegisterClass(&WndClass);
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow, const char *name, const char *name_show)
{
hwnd_main = CreateWindow(name, name_show, WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX,
GetSystemMetrics(SM_CXSCREEN) / 4, GetSystemMetrics(SM_CYSCREEN) / 4, WINDOW_WIDTH + 16, WINDOW_HEIGHT + 39, NULL, NULL, hInstance, NULL);
if (!hwnd_main)
return FALSE;
ShowWindow(hwnd_main, nCmdShow);
UpdateWindow(hwnd_main);
return TRUE;
}
VOID CALLBACK LocalTimerProc(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
InvalidateRect(hwnd_main, NULL, true);
}
dispix.h
#ifndef _DISPIX_H
#define _DISPIX_H
#include <windows.h>
class CDisPix
{
public:
unsigned char *framebuffer; //32位色的存储空间,大小为Height*Width*4字节
CDisPix(int height,int width);
~CDisPix();
void UpdatePixel(HDC hDC);//更新绘图
void SetRect(int y, int x, unsigned char b, unsigned char g, unsigned char r, int Halfheight, int Hlfwidth);
void SetHollowRect(int y, int x, unsigned char b, unsigned char g, unsigned char r, int Halfheight, int Hlfwidth);//中空矩阵
void SetPixel(int y, int x, unsigned char b, unsigned char g,unsigned char r);
void Fill(unsigned char b, unsigned char g, unsigned char r);
int GetHeight();
int GetWidth();
private:
int height;
int width;
};
#endif
dispix.cpp
#include "dispix.h"
#include <windows.h>
CDisPix::CDisPix(int height, int width)
{
int i, j;
this->height = height;
this->width = width;
framebuffer = (unsigned char*)malloc(height*width * 4);
Fill(255, 0, 0);
}
CDisPix::~CDisPix()
{
free(framebuffer);
}
void CDisPix::UpdatePixel(HDC hDC)
{
HDC memDC = CreateCompatibleDC(hDC);
HBITMAP bmp = CreateCompatibleBitmap(hDC, width, height);
SelectObject(memDC, bmp);
BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = width; //图像宽度
bmpInfo.bmiHeader.biHeight = -height; //图像高度
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 32; //32位色,b,g,r,保留,由于计算机存储是4字节对齐
bmpInfo.bmiHeader.biCompression = BI_RGB; //无压缩
bmpInfo.bmiHeader.biSizeImage = 0;
bmpInfo.bmiHeader.biXPelsPerMeter = 3000; //X方向每米的像素数,实际上没有作用
bmpInfo.bmiHeader.biYPelsPerMeter = 3000; //X方向每米的像素数,实际上没有作用
bmpInfo.bmiHeader.biClrUsed = 0; //0
bmpInfo.bmiHeader.biClrImportant = 0; //0
SetDIBits(hDC, bmp, 0, height, framebuffer, &bmpInfo, DIB_RGB_COLORS); //在内存中打图
BitBlt(hDC, 0, 0, width, height, memDC, 0, 0, SRCCOPY);
//StretchBlt(hDC,0, 0, 320, 240, memDC, 0, 0, 320, 240, SRCCOPY);//拉伸显示
}
void CDisPix::SetRect(int y, int x, unsigned char b, unsigned char g, unsigned char r, int Halfheight, int Hlfwidth)
{
int i, j, ct;
if (y + Halfheight >= height || y - Halfheight < 0) return;
if (x + Hlfwidth >= width || x - Hlfwidth < 0) return;
for (i = y - Halfheight; i <= y + Halfheight; i++)
{
for (j = x - Hlfwidth; j <= x + Hlfwidth; j++)
{
ct = (i * width * 4 + j * 4);
framebuffer[ct + 0] = b;
framebuffer[ct + 1] = g;
framebuffer[ct + 2] = r;
}
}
}
void CDisPix::SetHollowRect(int y, int x, unsigned char b, unsigned char g, unsigned char r, int Halfheight, int Hlfwidth)
{
int i, j, ct;
if (y + Halfheight >= height || y - Halfheight < 0) return;
if (x + Hlfwidth >= width || x - Hlfwidth < 0) return;
for (i = y - Halfheight; i <= y + Halfheight; i++)
{
ct = (i * width * 4 + (x - Hlfwidth) * 4);
framebuffer[ct + 0] = b;
framebuffer[ct + 1] = g;
framebuffer[ct + 2] = r;
ct = (i * width * 4 + (x + Hlfwidth) * 4);
framebuffer[ct + 0] = b;
framebuffer[ct + 1] = g;
framebuffer[ct + 2] = r;
}
for (j = x - Hlfwidth; j <= x + Hlfwidth; j++)
{
ct = ((y - Halfheight) * width * 4 + j * 4);
framebuffer[ct + 0] = b;
framebuffer[ct + 1] = g;
framebuffer[ct + 2] = r;
ct = ((y + Halfheight) * width * 4 + j * 4);
framebuffer[ct + 0] = b;
framebuffer[ct + 1] = g;
framebuffer[ct + 2] = r;
}
}
void CDisPix::SetPixel(int y, int x, unsigned char b, unsigned char g,unsigned char r)
{
if (y >= height || y < 0) return;
if (x >= width || x < 0) return;
int ct = (y * width * 4 + x * 4);
framebuffer[ct + 0] = b;
framebuffer[ct + 1] = g;
framebuffer[ct + 2] = r;
}
void CDisPix::Fill(unsigned char b, unsigned char g, unsigned char r)
{
int i, j, ct;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
ct = (i * width * 4 + j * 4);
framebuffer[ct + 0] = b;
framebuffer[ct + 1] = g;
framebuffer[ct + 2] = r;
}
}
}
int CDisPix::GetHeight()
{
return height;
}
int CDisPix::GetWidth()
{
return width;
}