SetDisplayMode 只是改变分辨率,为什么会导致程序全屏?

unhappyless 2008-10-21 03:06:15
如题
...全文
1450 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
having_hr 2008-10-28
  • 打赏
  • 举报
回复
我的这样写都没问题

#include "stdafx.h"
#include "MyDraw.h"

LPDIRECTDRAW7 lpDDraw7 = NULL; //----------DirectDraw 对象接口

LPDIRECTDRAWSURFACE7 lpDDprimary=NULL;
LPDIRECTDRAWSURFACE7 lpDDback=NULL;
DDSURFACEDESC2 ddsd;

int SCREEN_WIDTH = 1024; //屏幕宽度
int SCREEN_HEIGHT = 768; //屏幕高度
const int SCREEN_BPP = 32; //色深

int MyDrawInit(bool fullscreen)

{
LPDIRECTDRAW lpDDraw_temp; //临时指针指向创建的DirectDraw 对象,并用来查询最新的DirectDraw接口。

//------------------------------------------------------------------------------------------------------------------------------
if( FAILED( DirectDrawCreate( NULL, &lpDDraw_temp,NULL ) ) ) //创建Direct Draw对象,指针存放在lpDDraw_temp。
{
MessageBox(NULL,TEXT("DirectDraw Create error!"),TEXT("wrong!"),MB_OK);
return 0;
}


//------------------------------------------------------------------------------------------------------------------------------
if(FAILED(lpDDraw_temp->QueryInterface(IID_IDirectDraw7,(LPVOID*)&lpDDraw7))) //通过查询获得最新DirectDraw接口,把指针存放在 lpDDraw7中。
{
MessageBox(NULL,TEXT("DirectDraw QueryInterface error!"),TEXT("wrong!"),MB_OK);
return 0;
}

//------------------------------------------------------------------------------------------------------------------------------
if(fullscreen)
{
if(FAILED(lpDDraw7->SetCooperativeLevel(main_hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE))) // 设置协同等级,这里是全屏幕模式。
{
MessageBox(NULL,TEXT("DirectDraw SetCooperativeLevel error!"),TEXT("wrong!"),MB_OK);
return 0;
}

//------------------------------------------------------------------------------------------------------------------------------
if(FAILED(lpDDraw7->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0,0))) //设置游戏的显示模式,分辨率与色深。先尝试1024 x 768
{
MessageBox(NULL,TEXT("This display card does not support 1024x768x16."),TEXT("DirectDraw Sample"),MB_ICONERROR|MB_OK );

SCREEN_WIDTH = 800;
SCREEN_HEIGHT= 600;

if(FAILED(lpDDraw7->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0,0))) //设置游戏的显示模式,分辨率与色深。再尝试800 x 600
{
MessageBox(NULL,TEXT("DirectDraw SetDisplayMode error!"),TEXT("wrong!"),MB_OK);
return 0;
}
}
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount=1;
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|DDSCAPS_FLIP;

if(FAILED(lpDDraw7->CreateSurface(&ddsd,&lpDDprimary,NULL)))
{
MessageBox(NULL,TEXT("DirectDraw Create Primary Surface error!"),TEXT("wrong!"),MB_OK);
return 0;
}

ddsd.ddsCaps.dwCaps=DDSCAPS_BACKBUFFER;

if(FAILED(lpDDprimary->GetAttachedSurface(&ddsd.ddsCaps,&lpDDback)))
{
MessageBox(NULL,TEXT("DirectDraw Create Back Surface error!"),TEXT("wrong!"),MB_OK);
return 0;
}
}
else // 在窗口模式下的设置
{
if(FAILED(lpDDraw7->SetCooperativeLevel(main_hwnd,DDSCL_NORMAL))) // 设置协同等级,这里是窗口模式。
{
MessageBox(NULL,TEXT("DirectDraw SetCooperativeLevel error!"),TEXT("wrong!"),MB_OK);
return 0;
}
memset(&ddsd, 0,sizeof(ddsd)); //把结构体清零
ddsd.dwSize = sizeof(ddsd); //获得结构体大小
ddsd.dwFlags = DDSD_CAPS; //表明下面需要被填充的结构体参数
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; //设置表面的特性,这里把画面设为:主画面
if(FAILED(lpDDraw7->CreateSurface(&ddsd,&lpDDprimary,NULL))) //创建主表面,把设置好的ddsd结构参数传递给系统,并把创建好的主表面指针存放于lpDDprimary中
{
MessageBox(NULL,TEXT("DirectDraw Create Primary Surface error!"),TEXT("wrong!"),MB_OK);
return 0;
}

RECT rect; // 创立窗口模式的后备表面。
GetClientRect(main_hwnd, &rect); // 通过main_hwnd得到用户区范围
memset(&ddsd, 0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.dwWidth = rect.right - rect.left; // 该离屏表面的大小和用户区的大小一样
ddsd.dwHeight = rect.bottom - rect.top;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; // 把此后备表面设置为离屏表面

if(FAILED(lpDDraw7->CreateSurface(&ddsd, &lpDDback, NULL))) // 这次lpDDback指向的不是后备表面,而是离屏表面
{
MessageBox(NULL, TEXT("DirectDraw Create Window Surface error!"), TEXT("Wrong"), MB_OK);
return 0;
}
}




LPDIRECTDRAWCLIPPER pcClipper; //声明剪切板对象指针

if( FAILED( lpDDraw7->CreateClipper(0, &pcClipper, NULL))) //创建剪切板对象
return 0;

if( FAILED( pcClipper->SetHWnd( 0, main_hwnd))) //设置剪切板裁剪区域为指定(通过窗口句柄main_hwnd指定)窗口大小
{
pcClipper->Release();
return 0;
}

if(fullscreen)
{
if( FAILED( lpDDback->SetClipper(pcClipper))) //把制定好剪切板与后备缓冲表面关联
{
pcClipper->Release();
return 0;
}
}
else
{
if(FAILED(lpDDprimary->SetClipper(pcClipper)))
{
pcClipper->Release();
return 0;
}
}
pcClipper->Release();


//------------------------------------------------------------------------------------------------------------------------------
//请在这加入创建主表面的代码:

//------------------------------------------------------------------------------------------------------------------------------
return 1;
}

int MyDrawShut()
{
if(lpDDback)
{
lpDDback->Release();
lpDDback=NULL;
}
if(lpDDprimary)
{
lpDDprimary->Release();
lpDDprimary=NULL;
}

if(lpDDraw7)
{
lpDDraw7->Release();
lpDDraw7=NULL;
}


return 1;
}


LPDIRECTDRAWSURFACE7 DDLoadBitmap(LPCTSTR FileName)
{
HBITMAP hbmp; //位图的句柄
HDC hdc,dhdc; //设备环境的句柄
hbmp=(HBITMAP)::LoadImage(NULL,FileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE); //以文件形式读取位图给hbmp,

if(hbmp==NULL)
{
MessageBox(NULL,TEXT("读取位图失败!"),TEXT("Wrong!"),MB_OK);
return NULL;
}
BITMAP bm; //BITMAP结构
GetObject(hbmp,sizeof(bm),&bm); //得到hbmp的信息,存放在bm里

DDSURFACEDESC2 ddsd2; //建立表面结构信息ddsd2
LPDIRECTDRAWSURFACE7 lpdds; //建立一个理屏表面的指针lpdds
memset(&ddsd2,0,sizeof(ddsd2));
ddsd2.dwSize=sizeof(ddsd2);
ddsd2.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT; // 标明接着需要填充的表面内容
ddsd2.dwWidth=bm.bmWidth; // 表面的宽度等于位图的宽度
ddsd2.dwHeight=bm.bmHeight; // 表面的高度等于位图的高度
ddsd2.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY; //标明离屏表面| 存放于系统内存中

if(FAILED(lpDDraw7->CreateSurface(&ddsd2,&lpdds,NULL))) //创建离屏表面,并把该表面指针存放在lpdds中
{
MessageBox(NULL,TEXT("创建表面失败!"),TEXT("Wrong!"),MB_OK);
return 0;
}
hdc=::CreateCompatibleDC(NULL); //为当前屏幕创建与设备环境兼容的内存
::SelectObject(hdc,hbmp); //挑选位图对象hbmp到HDC中
lpdds->GetDC(&dhdc); //让离屏表面lpdds获得HDC句柄;
::BitBlt(dhdc,0,0,bm.bmWidth ,bm.bmHeight ,hdc,0,0,SRCCOPY);//把整个位图Blt 到离屏表面上
lpdds->ReleaseDC(dhdc); //释放dhdc资源
return lpdds; //完成离屏表面的建立并把返回该表面指针

}


int Clear(DWORD Color) //清屏函数
{
DDBLTFX ddbltfx; //建立DDBLTFX结构ddbltfx
memset(&ddbltfx,0,sizeof(DDBLTFX));
ddbltfx.dwSize=sizeof(ddbltfx);
ddbltfx.dwFillColor =Color; //为该绘图区填充颜色,这也就是游戏背景的颜色

lpDDback->Blt(NULL,NULL,NULL,DDBLT_WAIT|DDBLT_COLORFILL,&ddbltfx); //把ddbltfx Blt上后备表面,作清屏之用
return 1;
}



having_hr 2008-10-28
  • 打赏
  • 举报
回复
SetCooperativeLevel也没有设错的话,就很奇怪了
unhappyless 2008-10-23
  • 打赏
  • 举报
回复
// set display mode to 640x480x8
if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,0,0)))


640x480x8 这大小也不是全屏大小吧
帅得不敢出门 2008-10-22
  • 打赏
  • 举报
回复
这个改的又不是分辨率,而是显示模式
定义文件在显示出来的模式。可以设定(缩小放大)的级数:使到显示出来的页面为全画面、窗口的全宽度、使用真正的大小、安比例(缩小放大)或使用默认值
http://www.21andy.com/fpdf-manual/setdisplaymode.htm
unhappyless 2008-10-21
  • 打赏
  • 举报
回复
////////////////////////////////////////////////////////////

int Game_Init(void *parms = NULL, int num_parms = 0)
{
// this is called once after the initial window is created and
// before the main event loop is entered, do all your initialization
// here

// create IDirectDraw interface 7.0 object and test for error
if (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)))
return(0);

// set cooperation to normal since this will be a windowed app
lpdd->SetCooperativeLevel(main_window_handle, DDSCL_NORMAL);

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//需要:DDraw.lib

//把下面函数注释掉就不全屏了


//
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// set display mode to 640x480x8
if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,0,0)))
{
// error
return(0);
} // end if
// return success or failure or your own return code here
return(1);

} // end Game_Init

/////////////////////////////////////////////////////////////

int Game_Shutdown(void *parms = NULL, int num_parms = 0)
{
// this is called after the game is exited and the main event
// loop while is exited, do all you cleanup and shutdown here

// simply blow away the IDirectDraw4 interface
if (lpdd)
{
lpdd->Release();
lpdd = NULL;
} // end if

// return success or failure or your own return code here
return(1);

} // end Game_Shutdown

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{

WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
HDC hdc; // graphics device context

// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// save hinstance in global
hinstance_app = hinstance;

// register the window class
if (!RegisterClassEx(&winclass))
return(0);

// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"DirectDraw Initialization Demo", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // initial x,y
400,300, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
return(0);

// save main window handle
main_window_handle = hwnd;

// initialize game here
Game_Init();

// enter main event loop
while(TRUE)
{
// test if there is a message in queue, if so get it
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (msg.message == WM_QUIT)
break;

// translate any accelerator keys
TranslateMessage(&msg);

// send the message to the window proc
DispatchMessage(&msg);
} // end if

// main game processing goes here
Game_Main();

} // end while

// closedown game here
Game_Shutdown();

// return to Windows like this
return(msg.wParam);

} // end WinMain

///////////////////////////////////////////////////////////
unhappyless 2008-10-21
  • 打赏
  • 举报
回复
// DEMO6_1.CPP basic DirectDraw initialization demo

// INCLUDES ///////////////////////////////////////////////

#define WIN32_LEAN_AND_MEAN // just say no to MFC

#define INITGUID // make sure directX guids are included

#include <windows.h> // include important windows stuff
#include <windowsx.h>
#include <mmsystem.h>
#include <iostream> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>

#include <ddraw.h> // include directdraw

// DEFINES ////////////////////////////////////////////////

// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"

// default screen size
#define SCREEN_WIDTH 640 // size of screen
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 8 // bits per pixel
#define MAX_COLORS 256 // maximum colors

// TYPES //////////////////////////////////////////////////////

// basic unsigned types
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char UCHAR;
typedef unsigned char BYTE;

// MACROS /////////////////////////////////////////////////

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// initializes a direct draw struct
#define DD_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }

// GLOBALS ////////////////////////////////////////////////
HWND main_window_handle = NULL; // globally track main window
HINSTANCE hinstance_app = NULL; // globally track hinstance

// directdraw stuff

LPDIRECTDRAW7 lpdd = NULL; // dd object
LPDIRECTDRAWSURFACE7 lpddsprimary = NULL; // dd primary surface
LPDIRECTDRAWSURFACE7 lpddsback = NULL; // dd back surface
LPDIRECTDRAWPALETTE lpddpal = NULL; // a pointer to the created dd palette
LPDIRECTDRAWCLIPPER lpddclipper = NULL; // dd clipper
PALETTEENTRY palette[256]; // color palette
PALETTEENTRY save_palette[256]; // used to save palettes
DDSURFACEDESC2 ddsd; // a direct draw surface description struct
DDBLTFX ddbltfx; // used to fill
DDSCAPS2 ddscaps; // a direct draw surface capabilities struct
HRESULT ddrval; // result back from dd calls
DWORD start_clock_count = 0; // used for timing

// these defined the general clipping rectangle
int min_clip_x = 0, // clipping rectangle
max_clip_x = SCREEN_WIDTH-1,
min_clip_y = 0,
max_clip_y = SCREEN_HEIGHT-1;

// these are overwritten globally by DD_Init()
int screen_width = SCREEN_WIDTH, // width of screen
screen_height = SCREEN_HEIGHT, // height of screen
screen_bpp = SCREEN_BPP; // bits per pixel


char buffer[80]; // general printing buffer

// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
char buffer[80]; // used to print strings

// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
// return success
return(0);
} break;

case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);

// end painting
EndPaint(hwnd,&ps);

// return success
return(0);
} break;

case WM_DESTROY:
{

// kill the application, this sends a WM_QUIT message
PostQuitMessage(0);

// return success
return(0);
} break;

default:break;

} // end switch

// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

///////////////////////////////////////////////////////////

int Game_Main(void *parms = NULL, int num_parms = 0)
{
// this is the main loop of the game, do all your processing
// here

// for now test if user is hitting ESC and send WM_CLOSE
if (KEYDOWN(VK_ESCAPE))
SendMessage(main_window_handle,WM_CLOSE,0,0);

// return success or failure or your own return code here
return(1);

} // end Game_Main


65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧