游戏开发论坛

 找回密码
 立即注册
搜索
查看: 9288|回复: 20

一个关于3D场景绘制2D文字的问题

[复制链接]

23

主题

67

帖子

67

积分

注册会员

Rank: 2

积分
67
发表于 2005-4-18 14:48:00 | 显示全部楼层 |阅读模式
不知道为什么,编译能通过,但是运行的结果却没有显示出字来,高手帮忙看下我的程序:
首先定义了一个font类:
#include <string>
#include <windows.h>
#include <d3dx9.h>
#include "DXUT.h"
//#include "dxstdafx.h"
#include "DXUT.h"
//#include "DXUTenum.h"
//#include "DXUTgui.h"
//#include "DXUTMesh.h"
//#include "DXUTSettingsDlg.h"
#pragma warning(default: 4995)

using namespace std;

//typedef basic_string<wchar_t> wstring;

class font{
public:
        font();
        ~font();
        bool showText(wstring s,int bx,int by,LPDIRECT3DDEVICE9 m_pd3dDevice);
                //这个接口需要指明“字符串”,“字符开始绘制的坐标x”,“字符开始绘制的坐标y”
                //D3D对象指针
        //bool fontInit();
        //void fontDestroy();
        //HFONT m_font;
       
       
};
类定义:
#include "font.h"

font::font(){

        //HFONT m_font;
        //m_font=CreateFont;
}
font::~font(){


}


bool font::showText(string s,int bx,int by,LPDIRECT3DDEVICE9 m_pd3dDevice,RECT rect){
        char *pstrFont="Tunga";
       
        if (FAILED(D3DXCreateFont(m_pd3dDevice,0,0,FW_DONTCARE,
                                                                                0,0,0,0,0,
                                                                                0,pstrFont,&ppfont)))return 0;
        ppfont->DrawText(0,s.c_str(),sizeof(s),&rect,DT_SINGLELINE,0xffffffff);
               
    }

23

主题

67

帖子

67

积分

注册会员

Rank: 2

积分
67
 楼主| 发表于 2005-4-18 14:53:00 | 显示全部楼层

Re: 一个关于3D场景绘制2D文字的问题

#pragma warning(disable: 4995)
#include <stdio.h>
#include <shlobj.h>
#include <windows.h>
#include <string>
#include "font.h"
#include <d3d9.h>
#include <d3dx9.h>
#include "DXUTmisc.h"
#include <assert.h>
#include <strsafe.h>
using namespace std;


/*#pragma warning(disable: 4995)
#include <stdio.h>
#include <shlobj.h>
#include "resource.h"
#include "ConfigDatabase.h"
#include "ConfigManager.h"*/
#pragma warning(default: 4995)
LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
ID3DXSprite*            g_pTextSprite = NULL;
ID3DXFont*              g_pFont = NULL;
//two interface we need

typedef basic_string<wchar_t> string;


//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object, which is needed to create the D3DDevice.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;
     
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }
       
        //D3DXCreateSprite( g_pd3dDevice, &g_pTextSprite );
        D3DXCreateFont( g_pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
                         OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
                         L"Arial", &g_pFont );//创建2D字体
     return S_OK;
}




//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    if( g_pd3dDevice != NULL)
        g_pd3dDevice->Release();

    if( g_pD3D != NULL)
        g_pD3D->Release();
}


//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    if( NULL == g_pd3dDevice )
        return;
        const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
    char *strText="hello!";
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
       
   g_pFont->DrawText(0,strText,strlen(strText),&stRect,DT_SINGLELINE,0xffffffff);
               
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice-&gtresent( NULL, NULL, NULL, NULL );
}

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;

        case WM_PAINT:
            Render();
            ValidateRect( hWnd, NULL );
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}



INT WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,INT){
        // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      L"Font Demo", NULL };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( L"Font Demo", L"Font Demo",
                              WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );
    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Show the window
        ShowWindow( hWnd, SW_SHOWDEFAULT );
        UpdateWindow( hWnd );

        // Enter the message loop
        MSG msg;
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }

    UnregisterClass( L"Tank", wc.hInstance );
    return 0;
        return 0;
}

132

主题

1341

帖子

1341

积分

金牌会员

Rank: 6Rank: 6

积分
1341
发表于 2005-4-18 18:12:00 | 显示全部楼层

Re:一个关于3D场景绘制2D文字的问题

看看stRect有没有错吧

23

主题

67

帖子

67

积分

注册会员

Rank: 2

积分
67
 楼主| 发表于 2005-4-20 09:35:00 | 显示全部楼层

Re:一个关于3D场景绘制2D文字的问题

VOID Render()
{
    if( NULL == g_pd3dDevice )
        return;
   
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );
        //g_pd3dDevice->Clear( 0, &m_rect, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
        font m_font;
       
   
    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {

                RECT stRect={20,20,400,400};

                m_font.showText("hellohello",100,100,g_pd3dDevice,stRect);
                               
        g_pd3dDevice->EndScene();
    }
    g_pd3dDevice-&gtresent( NULL, NULL, NULL, NULL );
}

23

主题

67

帖子

67

积分

注册会员

Rank: 2

积分
67
 楼主| 发表于 2005-4-20 09:35:00 | 显示全部楼层

Re:一个关于3D场景绘制2D文字的问题

问题依然如故,我的stRect吗?

23

主题

67

帖子

67

积分

注册会员

Rank: 2

积分
67
 楼主| 发表于 2005-4-20 09:54:00 | 显示全部楼层

Re:一个关于3D场景绘制2D文字的问题

在线等。。。

132

主题

1341

帖子

1341

积分

金牌会员

Rank: 6Rank: 6

积分
1341
发表于 2005-4-20 12:28:00 | 显示全部楼层

Re:一个关于3D场景绘制2D文字的问题

LPD3DXFONT pFont;
HFONT Font;
RECT rect={0,0,200,200};
Font=CreateFont(40,0,0,0,FW_BOLD,0,0,0,0,0,0,0,0,"华文楷体");//华文行楷可以改为其他
D3DXCreateFont(g_pd3dDevice,Font,&pFont);
pFont->DrawTextA("hdm2968",strlen("hdm2968"),&rect,DT_TOP|DT_CENTER,0x00000000);

23

主题

67

帖子

67

积分

注册会员

Rank: 2

积分
67
 楼主| 发表于 2005-4-20 16:55:00 | 显示全部楼层

Re:一个关于3D场景绘制2D文字的问题

还是不行啊,编译出错,提示:
------ Build started: Project: CreateDevice, Configuration: Debug Win32 ------
Compiling...
CreateDevice.cpp
e:\vc.net\CreateDevice\CreateDevice.cpp(114) : error C2660: 'D3DXCreateFontA' : function does not take 3 arguments
e:\vc.net\CreateDevice\CreateDevice.cpp(115) : error C2660: 'ID3DXFont:rawTextA' : function does not take 5 arguments

Build log was saved at "file://e:\vc.net\CreateDevice\Debug\BuildLog.htm"
CreateDevice - 2 error(s), 0 warning(s)
---------------------- Done ----------------------

    Build: 0 succeeded, 1 failed, 0 skipped
这次我用的是DX9 sample中的“create device”,只不过在render()函数中加入:
。。。。。。。。。。//render。。。。。。。
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Rendering of scene objects can happen here
   
        // End the scene

                LPD3DXFONT pFont;
                HFONT Font;
                RECT rect={0,0,200,200};
                Font=CreateFont(40,0,0,0,FW_BOLD,0,0,0,0,0,0,0,0,"华文楷体");//华文行楷可以改为其他
                D3DXCreateFont(g_pd3dDevice,Font,&pFont);
                pFont->DrawTextA("hdm2968",strlen("hdm2968"),&rect,DT_TOP|DT_CENTER,0x00000000);

        g_pd3dDevice->EndScene();
    }
。。。。

132

主题

1341

帖子

1341

积分

金牌会员

Rank: 6Rank: 6

积分
1341
发表于 2005-4-20 21:11:00 | 显示全部楼层

Re:一个关于3D场景绘制2D文字的问题

#include<d3d9.h>
#include<d3dx9.h>
我用的是dx9.0 b,dx的很多更新都会把createfont和drawtext改了,你对着帮助文件修改下吧。

23

主题

67

帖子

67

积分

注册会员

Rank: 2

积分
67
 楼主| 发表于 2005-4-21 09:55:00 | 显示全部楼层

Re:一个关于3D场景绘制2D文字的问题

好了,可以了,代码:
。。。
D3DXFONT_DESC d3dFont;
LPD3DXFONT sfont;
memset(&d3dFont,0,sizeof(d3dFont));
        d3dFont.Height=25;
        d3dFont.Width=12;
        d3dFont.Weight=500;
        d3dFont.Italic=FALSE;
        d3dFont.CharSet=DEFAULT_CHARSET;
        LPD3DXFONT sfont;
        D3DXCreateFontIndirect(g_pd3dDevice,&d3dFont,&sfont);
sfont->DrawText(NULL,"Hello World",-1,&rect,DT_TOP|DT_LEFT,0xffffffff);
。。。。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-20 02:05

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表