C++ miniwin actualizado
Enviado por Juan Pablo Bedoya Benavides • 10 de Junio de 2020 • Prácticas o problemas • 5.133 Palabras (21 Páginas) • 255 Visitas
/*
* MiniWin: Un mini-conjunto de funciones para abrir una ventana, pintar en
* ella y detectar la presión de algunas teclas. Básicamente para hacer
* juegos sencillos.
*
* (c) Pau Fernández, licencia MIT: http://es.wikipedia.org/wiki/MIT_License
*/
// VERSION: 0.2.2
#if defined(_WIN32)
// Windows ////////////////////////////////////////////////////////////////////////////
#include <fstream>
#include <sstream>
#include <queue>
#include <math.h>
#include <process.h>
#include <windows.h>
#include <windowsx.h>
#define MINIWIN_SOURCE
#include "miniwin.h"
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "programa palindromos";
// Variables globales //////////////////////////////////////////////////////////
HWND hWnd; // ventana principal
HBITMAP hBitmap; // bitmap para pintar off-screen
int iWidth = 400; // ancho de la ventana
int iHeight = 300; // alto de la ventana
HDC hDCMem = NULL; // Device Context en memoria
std::queue<int> _teclas; // cola de teclas
bool _raton_dentro; // el raton está dentro del 'client area'
int _xraton, _yraton; // posicion del raton
bool _bot_izq, _bot_der;// botones izquierdo y derecho
////////////////////////////////////////////////////////////////////////////////
std::ostream& log() {
#if defined(DEBUG)
static std::ofstream _log("log.txt");
#else
static std::stringstream _log;
_log.str(""); // lo borra
#endif
return _log;
}
VOID Thread(PVOID pvoid) {
Sleep(10); // FIXME
_main_();
}
void maybe_call_main() {
static bool started = false;
if (!started) {
_beginthread(Thread, 0, NULL); // Llama a 'main' (realmente '_main_')
started = true;
}
}
void frame_real(int w, int h, int& rw, int &rh) {
RECT frame = { 0, 0, w, h };
AdjustWindowRect(&frame, WS_POPUPWINDOW, TRUE);
rw = frame.right - frame.left;
rh = frame.bottom - frame.top;
}
void newMemDC(int w, int h) {
if (hDCMem != NULL) {
DeleteObject(hBitmap);
DeleteDC(hDCMem);
}
log() << "New MemDC\n";
HDC hDC = GetDC(hWnd);
hDCMem = CreateCompatibleDC(hDC);
hBitmap = CreateCompatibleBitmap (hDC, w, h);
SelectObject(hDCMem, hBitmap);
SetBkMode(hDCMem, OPAQUE);
}
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
static WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 10;
wincl.cbWndExtra = 10;
wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW);
if (!RegisterClassEx (&wincl))
return 0;
int w, h;
frame_real(iWidth, iHeight, w, h);
hWnd = CreateWindowEx (
...