为全屏游戏编写代码 [英] writing code for fullscreen game

查看:84
本文介绍了为全屏游戏编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图创建一个全屏窗口缓冲区,可以写入点击冒险游戏,但我发现覆盖Windows任务栏的问题,实际上使用SetDIBitsToDevice显示像素是这里
代码:

hello, i trying to create a fullscreen window buffer which can be written to for a point and click adventure game but i am finding problems with overwriting the windows taskbar and actually getting the pixels to display using SetDIBitsToDevice here is the code:

//原住民衰变Demo.cpp:定义申请的入口点。

//



#include" stdafx.h"

#include" Aboriginal Decay Demo.h"



#define MAX_LOADSTRING 100



//全局变量:

HINSTANCE hInst;                                  //当前实例

WCHAR szTitle [MAX_LOADSTRING];                   //标题栏文本

WCHAR szWindowClass [MAX_LOADSTRING];            //主窗口类名称

FILE * debugfile = NULL;

int screenxsize,screenysize;

char * screenbuffer;

BITMAPINFO screenbitmapinfo;



//此代码模块中包含的函数的前向声明:

ATOM     ;            MyRegisterClass(HINSTANCE hInstance);

BOOL                InitInstance(HINSTANCE,int);
$
LRESULT CALLBACK   的WndProc(HWND,UINT,WPARAM,LPARAM);



INT APIENTRY wWinMain(标签:APIWinHTTP HINSTANCE的hInstance,

    _In_opt_ HINSTANCE hPrevInstance,

   标签:APIWinHTTP LPWSTR    lpCmdLine,

   标签:APIWinHTTP INT    &NBSP ;  nCmdShow)

{

     UNREFERENCED_PARAMETER(hPrevInstance);

     UNREFERENCED_PARAMETER(lpCmdLine);



     int x,y;

     // TODO:将代码放在此处。

     fopen_s(& debugfile," debug.txt"," w");

     if(debugfile == NULL){

        返回FALSE;

    }

     fprintf_s(debugfile," opened debugfile ... \ n");

     fflush(debugfile);

     screenxsize = GetSystemMetrics(SM_CXFULLSCREEN);

     screenysize = GetSystemMetrics(SM_CYFULLSCREEN);    // SPI_GetWorkArea有SystemParametersInfo允许任务栏

     fprintf_s(debugfile," screenxsize =%d,screenysize =%d \ n",screenxsize,screenysize);

     fflush(debugfile);

     if((screenbuffer =(char *)malloc(screenxsize * screenysize * 3))== NULL){

         fprintf_s(debugfile,"无法创建screenbuffer ... \ n");

         fflush(debugfile);

        返回FALSE;

    }

     for(y = 0; y< screenysize; y ++){

   &NBSP;&NBSP;&NBSP;   for(x = 0; x< screenxsize; x ++){

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   screenbuffer [(x + screenxsize * y)* 3] = 0;

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   screenbuffer [(x + screenxsize * y)* 3 + 1] = 0;

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   screenbuffer [(x + screenxsize * y)* 3 + 2] = 255;

   &NBSP;&NBSP;&NBSP;  }

    }

     screenbitmapinfo.bmiColors [0] .rgbBlue = 0;

     screenbitmapinfo.bmiColors [0] .rgbGreen = 0;

     screenbitmapinfo.bmiColors [0] .rgbRed = 0;

     screenbitmapinfo.bmiColors [0] .rgbReserved = 0;

     screenbitmapinfo.bmiHeader.biBitCount = 24;

     screenbitmapinfo.bmiHeader.biClrImportant = 0;

     screenbitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFO);

     screenbitmapinfo.bmiHeader.biWidth = screenxsize;

     screenbitmapinfo.bmiHeader.biHeight = screenysize;

     screenbitmapinfo.bmiHeader.biPlanes = 1;

     screenbitmapinfo.bmiHeader.biCompression = BI_RGB;

     screenbitmapinfo.bmiHeader.biSizeImage = 0;

     screenbitmapinfo.bmiHeader.biXPelsPerMeter = 0;

     screenbitmapinfo.bmiHeader.biYPelsPerMeter = 0;

     screenbitmapinfo.bmiHeader.biClrUsed = 0;

     //初始化全局字符串

     LoadStringW(hInstance,IDS_APP_TITLE,szTitle,MAX_LOADSTRING);

     LoadStringW(hInstance,IDC_ABORIGINALDECAYDEMO,szWindowClass,MAX_LOADSTRING);

     MyRegisterClass(hInstance);



     //执行应用程序初始化:

     if(!InitInstance(hInstance,nCmdShow))

     {

   &NBSP;&NBSP;&NBSP;  返回FALSE;

    }
$


     HACCEL hAccelTable = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDC_ABORIGINALDECAYDEMO));
$


     MSG msg;



$


    //主消息循环:

    while(GetMessage(& msg,nullptr,0,0))

    {

        if(!TranslateAccelerator(msg.hwnd,hAccelTable,& msg))

        {

            TranslateMessage(& msg);

            DispatchMessage(& msg);

        }
    }


    return(int)msg.wParam;

}



$


/ /
$
// 功能:MyRegisterClass()

//

// 目的:注册窗口类。

//

ATOM MyRegisterClass(HINSTANCE hInstance)

{

  &NBSP;&NBSP; WNDCLASSEXW wcex;



    wcex.cbSize = sizeof(WNDCLASSEX);



    wcex.style&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; = CS_HREDRAW | CS_VREDRAW;

    wcex.lpfnWndProc&NBSP;&NBSP;&NBSP; = WndProc;

    wcex.cbClsExtra&NBSP;&NBSP;&NBSP;&NBSP; = 0;

    wcex.cbWndExtra&NBSP;&NBSP;&NBSP;&NBSP; = 0;

    wcex.hInstance&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; = hInstance;

    wcex.hIcon&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; = NULL;

    wcex.hCursor&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; = LoadCursor(nullptr,IDC_ARROW);

    wcex.hbrBackground&NBSP; =(HBRUSH)(COLOR_WINDOW + 1);

     wcex.lpszMenuName = NULL;   // MAKEINTRESOURCEW(IDC_ABORIGINALDECAYDEMO);

    wcex.lpszClassName&NBSP; = szWindowClass;

    wcex.hIconSm&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; = LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_SMALL));



    return RegisterClassExW(& wcex);

}



//

//  功能:InitInstance(HINSTANCE,int)

//

//  目的:保存实例句柄并创建主窗口

//

//  评论:

//

//       在此功能中,我们将实例句柄保存在全局变量中,并且
//       创建并显示主程序窗口。

//

BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)

{

   hInst = hInstance; //在我们的全局变量中存储实例句柄



   HWND的HWND = CreateWindowW(szWindowClass,szTitle,WS_POPUP,

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; 0,0,screenxsize,screenysize,nullptr,nullptr,的hInstance,nullptr);



   if(!hWnd)

   {

     返回FALSE;

   }


   ShowWindow(hWnd,nCmdShow);

   UpdateWindow(hWnd);



  返回TRUE;

}


//

// 功能:WndProc(HWND,UINT,WPARAM,LPARAM)

//

// 目的:处理主窗口的消息。

//

//  WM_COMMAND&NBSP; - 处理申请菜单

//  WM_PAINT&NBSP;&NBSP;&NBSP; - 绘制主窗口

//  WM_DESTROY&NBSP; - 发布退出消息并返回

//

//

LRESULT CALLBACK WndProc(HWND hWnd,UINT消息,WPARAM wParam,LPARAM lParam )

{

   开关(留言)

    {

   案例WM_PAINT:

        {

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; fprintf_s(debugfile,"处理WM_PAINT ... \ n");

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   fflush(debugfile);

            PAINTSTRUCT ps;

            HDC hdc = BeginPaint(hWnd,& ps);

            // TODO:在这里添加任何使用hdc的绘图代码...

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   SetDIBitsToDevice(hdc,0,0,screenxsize,screenysize,0,0,0,0,screenbuffer,& screenbitmapinfo,0);

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   EndPaint(hWnd,& ps);

        }
       休息;

   案例WM_DESTROY:

        PostQuitMessage(0);

       休息;

   默认值:

       返回DefWindowProc(hWnd,message,wParam,lParam);

    }
   返回0;

}

// Aboriginal Decay Demo.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Aboriginal Decay Demo.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
FILE *debugfile = NULL;
int screenxsize, screenysize;
char *screenbuffer;
BITMAPINFO screenbitmapinfo;

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    int x, y;
    // TODO: Place code here.
    fopen_s(&debugfile, "debug.txt", "w");
    if (debugfile == NULL) {
        return FALSE;
    }
    fprintf_s(debugfile, "opened debugfile...\n");
    fflush(debugfile);
    screenxsize = GetSystemMetrics(SM_CXFULLSCREEN);
    screenysize = GetSystemMetrics(SM_CYFULLSCREEN);    // there is SystemParametersInfo for SPI_GetWorkArea to allow taskbar
    fprintf_s(debugfile, "screenxsize=%d, screenysize=%d\n", screenxsize, screenysize);
    fflush(debugfile);
    if ((screenbuffer = (char *)malloc(screenxsize*screenysize * 3)) == NULL) {
        fprintf_s(debugfile, "cannot create screenbuffer...\n");
        fflush(debugfile);
        return FALSE;
    }
    for (y = 0; y < screenysize; y++) {
        for (x = 0; x < screenxsize; x++) {
            screenbuffer[(x + screenxsize * y) * 3] = 0;
            screenbuffer[(x + screenxsize * y) * 3 + 1] = 0;
            screenbuffer[(x + screenxsize * y) * 3 + 2] = 255;
        }
    }
    screenbitmapinfo.bmiColors[0].rgbBlue = 0;
    screenbitmapinfo.bmiColors[0].rgbGreen = 0;
    screenbitmapinfo.bmiColors[0].rgbRed = 0;
    screenbitmapinfo.bmiColors[0].rgbReserved = 0;
    screenbitmapinfo.bmiHeader.biBitCount = 24;
    screenbitmapinfo.bmiHeader.biClrImportant = 0;
    screenbitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFO);
    screenbitmapinfo.bmiHeader.biWidth = screenxsize;
    screenbitmapinfo.bmiHeader.biHeight = screenysize;
    screenbitmapinfo.bmiHeader.biPlanes = 1;
    screenbitmapinfo.bmiHeader.biCompression = BI_RGB;
    screenbitmapinfo.bmiHeader.biSizeImage = 0;
    screenbitmapinfo.bmiHeader.biXPelsPerMeter = 0;
    screenbitmapinfo.bmiHeader.biYPelsPerMeter = 0;
    screenbitmapinfo.bmiHeader.biClrUsed = 0;
    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_ABORIGINALDECAYDEMO, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance(hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ABORIGINALDECAYDEMO));

    MSG msg;



    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = NULL;
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;   // MAKEINTRESOURCEW(IDC_ABORIGINALDECAYDEMO);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_POPUP,
      0, 0, screenxsize, screenysize, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:
        {
            fprintf_s(debugfile, "processing WM_PAINT...\n");
            fflush(debugfile);
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            SetDIBitsToDevice(hdc, 0, 0, screenxsize, screenysize, 0, 0, 0, 0, screenbuffer, &screenbitmapinfo, 0);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

推荐答案

看看这是否有帮助 -
如何使用全屏窗口覆盖任务栏?
See if this helps - How do I cover the taskbar with a fullscreen window?


这篇关于为全屏游戏编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆