我正在开发一个简单的游戏来测试一些东西,我正在使用一个名为DX库的东西,它基本上是一个简化Direct X的库。无论如何,我正在使用Visual Studio 2013,我调试并运行它工作得很好。我根本没有改变代码,几分钟后回到它,在我在同一时间在Visual Studio中的另一个项目中编写的其他程序中执行了一些操作后,我再次运行它窗户打开的时间,它是完全黑色的,它在那里坐了几秒钟,然后关闭。我确保它在最后一次运行时正确关闭,甚至关闭并重新启动visual studio,它仍然做同样的事情。我不知道这是不是代码(虽然我上次运行时没有触及它)或者它是一个视觉工作室的东西。由于我使用的是Dx库,因此可能很难遵循代码,但我会将其全部放在下面。
的main.cpp
#include "main.h"
//Global variables
int g_lasttime = 0; //time of last calculation.
float g_frametime = 0; //amount of time for one loop.
int g_timerstart; //variable used by timer.
GameState g_gamestate = GAME_TITLE;
int g_gametitleimg; //title image
int g_heroimg; //character image
float g_hx, g_hy; //coordinates.
//Button
BOOL g_akey_prev; //previous state of A button
//font
int g_middlefont; //medium sized font.
int g_largefont; //large sized font.
int WINAPI WinMain(HINSTANCE h1, HINSTANCE hp, LPSTR lpC, int nC)
{
//Setting Window Mode
ChangeWindowMode(TRUE);
//Set Window Size.
SetGraphMode(800, 600, 32);
//Initiating DxLib
if (DxLib_Init() == -1)return -1;
//Loading image
if (LoadGameImage() == FALSE) return -1;
g_gametitleimg = LoadGraph("media\\Comp Title.png");
g_heroimg = LoadGraph("media\\g7.png");
g_middlefont = CreateFontToHandle("メリオ", 42, -1, DX_FONTTYPE_ANTIALIASING);
SetDrawScreen(DX_SCREEN_BACK);
g_lasttime = GetNowCount() & INT_MAX; //registers current time.
while (ProcessMessage() == 0 && CheckHitKey(KEY_INPUT_ESCAPE) == 0)
{
//Calculating time for 1 loop.
int curtime = GetNowCount();
g_frametime = (float)(curtime - g_lasttime) / 1000.0f;
g_lasttime = curtime;
ClearDrawScreen();
//Changing screen drawing functions
switch (g_gamestate)
{
case GAME_TITLE:
DrawGameTitle();
break;
//case GAME_CUT_SCENE:
//DrawCutScene();
//break;
case GAME_MAIN:
DrawGameMain();
break;
case GAME_CLEAR:
DrawGameClear();
break;
case GAME_OVER:
DrawGameOver();
break;
default:
break;
}
ScreenFlip();
}
WaitKey();
//Stopping DxLib
DxLib_End();
return 0;
}
//Draw Title
void DrawGameTitle()
{
DrawBox(0, 0, 800, 600, GetColor(225, 225, 225), TRUE);
DrawGraph(0, 0, g_gametitleimg, TRUE);
int ghandle = LoadGraph("media\\titlegirl.png");
DrawGraph(300, 200, ghandle, TRUE);
//Displaying text
DrawStringToHandle(50, 300, "Press Z To Start", GetColor(255,0,255),g_middlefont);
DrawStringToHandle(50, 360, "Use Arrow Keys To Move", GetColor(0, 0, 0), g_middlefont);
//Check if key is pressed
int key = GetJoypadInputState(DX_INPUT_KEY_PAD1);
if (IsAKeyTrigger(key) == TRUE)g_gamestate = GAME_MAIN;
}
//Main Game
void DrawGameMain()
{
GameMain();
}
//Draw Clear screen
void DrawGameClear()
{
}
//Draw Game Over screen
void DrawGameOver()
{
//DrawStringToHandle(100, 600, "Wait to return to Main Menu", GetColor(255, 150, 255), g_middlefont);
int ghandle = LoadGraph("media\\gameover.png");
DrawGraph(800, 600, ghandle, TRUE);
//returns to main menu after 5 seconds.
if (g_lasttime - g_timerstart > 5000)g_gamestate = GAME_TITLE;
}
//Key Trigger Proccess
BOOL IsAKeyTrigger(int key)
{
if (key & PAD_INPUT_A)
{
if (g_akey_prev == FALSE)
{
g_akey_prev = TRUE;
return TRUE;
}
}
else
{
g_akey_prev = FALSE;
}
return FALSE;
}
main.h
#ifndef _MAIN_H_
#define _MAIN_H_
#include <DxLib.h>
#include "gamemain.h"
#include "loading.h"
//Global variables
extern int g_lasttime; //time of last calculation.
extern float g_frametime; //amount of time for one loop.
extern int g_timerstart; //variable used by timer.
enum GameState{
GAME_TITLE, GAME_MAIN,
//GAME_CUT_SCENE,
GAME_CLEAR, GAME_OVER
};
extern GameState g_gamestate;
extern int g_gametitleimg; //title image
extern int g_heroimg; //character image
extern float g_hx, g_hy; //coordinates.
//Button
extern BOOL g_akey_prev; //previous state of A button
//font
extern int g_middlefont; //medium sized font.
extern int g_largefont; //large sized font.
//Declaring function prototypes
extern void DrawGameTitle();
extern void DrawGameMain();
//extern void DrawCutScene();
extern void DrawGameClear();
extern void DrawGameOver();
extern BOOL IsAKeyTrigger(int key);
#endif
gamemain.h
#ifndef _GAMEMAIN_H_
#define _GAMEMAIN_H_
#include <DxLib.h>
#include "main.h"
#define IMG_CHIP_SIZE 50
#define MAP_WIDTH 16
#define MAP_HEIGHT 12
void GameMain();
void DrawMap();
#endif
gamemain.cpp
void GameMain()
{
DrawMap();
//Character Movement
int key = GetJoypadInputState(DX_INPUT_KEY_PAD1);
float mv = 80.0f*g_frametime; //calc for movement.
if (key&PAD_INPUT_UP) g_hy -= mv;
if (key&PAD_INPUT_DOWN) g_hy += mv;
if (key&PAD_INPUT_LEFT) g_hx -= mv;
if (key&PAD_INPUT_RIGHT) g_hx += mv;
DrawGraph(g_hx, g_hy, g_heroimg, TRUE);
//Check if Z is pressed
if (IsAKeyTrigger(key) == TRUE)
{
g_gamestate = GAME_OVER;
g_timerstart = g_lasttime; //set timer
}
}
void DrawMap()
{
for (int y = 0; y < MAP_HEIGHT; y++)
{
for (int x = 0; x < MAP_WIDTH; x++)
{
DrawGraph(x*IMG_CHIP_SIZE, y*IMG_CHIP_SIZE, g_imghandles.field, FALSE);
}
}
}
loading.h
#ifndef _LOADING_H_
#define _LOADING_H_
#include <DxLib.h>
struct ImageHandles
{
int field; //background
};
extern ImageHandles g_imghandles;
BOOL LoadGameImage();
#endif
loading.cpp
#include "loading.h"
ImageHandles g_imghandles;
BOOL LoadGameImage()
{
g_imghandles.field = LoadGraph("media\\bgbrick.png");
if (g_imghandles.field == -1) return FALSE;
return TRUE;
}