Jump to content

  • Log In with Google      Sign In   
  • Create Account


precious roy

Member Since 30 Oct 2007
Offline Last Active Feb 11 2013 10:39 AM

Topics I've Started

(Fixed)(Beginner) How to use D3DXCreateTextureFromFile to load a texture

08 February 2013 - 07:53 AM

Hello,

 

i  have been looking for an answer to this question for the past 2 days and have only been able to find one that sometimes works. i will start by showing my code. the code in this case loads a model and continues to load the materials and textures.

 

void LoadModel(LPWSTR FileName, LPWSTR FileLocation)
{
    LPD3DXBUFFER bufShipMaterial;
	

    D3DXLoadMeshFromX((std::wstring(FileLocation) + FileName).c_str(),    // load this file
                      D3DXMESH_SYSTEMMEM,    // load the mesh into system memory
                      d3ddev,    // the Direct3D Device
                      NULL,    // we aren't using adjacency
                      &bufShipMaterial,    // put the materials here
                      NULL,    // we aren't using effect instances
                      &numMaterials,    // the number of materials in this model
                      &ModelMesh);    // put the mesh here

    // retrieve the pointer to the buffer containing the material information
    D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();

    // create a new material buffer for each material in the mesh
    material = new D3DMATERIAL9[numMaterials];
	texture = new LPDIRECT3DTEXTURE9[numMaterials];

    for(DWORD i = 0; i < numMaterials; i++)    // for each material...
    {
        material[i] = tempMaterials[i].MatD3D;    // get the material info
        material[i].Ambient = material[i].Diffuse;    // make ambient the same as diffuse


	//if there is a texture to load, load it
	if(FAILED(D3DXCreateTextureFromFileA(d3ddev, 
                  tempMaterials[i].pTextureFilename,&texture[i])))
		texture[i] = NULL;    // if there is no texture, set the texture to NULL
    }
}

The code above works fine to a point. when the texture is not in the same folder as the executable it will not lode it. you can see that when i load the texture i add the FileLocation to the Filename and this works like a charm. but now i want to do the same when loading my textures and this is where the problem start. i wiped up this, it probably well i know its not the best way of doing this because this gives me an error 2 out of the 3 times running the program

 

		if(tempMaterials[i].pTextureFilename != NULL)
		{
			//converting the lpstw from pTextureFilename to lpwstr
			char* convertMe = new char[sizeof(tempMaterials[i].pTextureFilename)];
			strcpy(convertMe,tempMaterials[i].pTextureFilename);
			int buffSize = (int)strlen(convertMe) + 1;
			LPWSTR TestureName = new wchar_t[buffSize];
			MultiByteToWideChar(CP_ACP, 0, convertMe, buffSize, TestureName, buffSize);
			std::wstring test = (std::wstring(FileLocation) + TestureName);

			//if there is a texture to load, load it
			if(FAILED(D3DXCreateTextureFromFile(d3ddev, test.c_str(), &texture[i])))
				texture[i] = NULL;    // if there is no texture, set the texture to NULL
		}
		else
			texture[i] = NULL;

 

 

the error that it gives is "Attempted to read or write protected memory. This is often an indication that other memory is corrupt.". anyways what i'm doing here is converting a LPSTR string to LPWSTR and then combine them, just like i did withe the model file.

 

now i know that the problem is somewhere in the converting of the string, i guess my question is how am i supposed to do this ?


Basic networking question.

05 May 2010 - 08:57 AM

hey every one. I am very new to networking in games.. and I need a couple of pointers. i'm working on a simple rpg style Network game(mmo) just for fun and to learn how things work. I want the server to be able to presses up to 900 clients with ease. but I don't really know the right way to handle client. what would be better. control a character and send cords to server or send possible next position to server and see if you can move or not ?

C++ text to int question

20 May 2009 - 07:37 AM

Hi every one, I’ve just started programming in c++ and was wondering how would you convert a in to string an back ? I’m programming without the standard libraries. any ideas. Roy

Ultimate Game Programming with DirectX

09 March 2009 - 03:57 AM

Hi This is probably the wrong section but I didn’t know where to put it... so does any one have this book (Ultimate Game Programming with DirectX ). And is it a good book??? p-roy

code access problem

12 May 2008 - 11:47 PM

Hi I was hoping some on can help me with this problem. I want to access the (SetText) from a different class the (SetText) puts text in to a text box. now I open a new thread in that class and it is suppose to insert some text in to my text box. But I can access it, I can call my (SetText) public void but I sit cant access and if I call it public static void then my (SetText) cant access the textbox any more
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;


namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        delegate void SetTextCallback(string text);
        private Thread demoThread = null;
        private Thread demoThread2 = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void ClientButton_Click_1(object sender, EventArgs e)
        {
            this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe));
            this.demoThread2 = new Thread(new ThreadStart(TestInsert.InsertTest));
            this.demoThread.Start();
            this.demoThread2.Start();
        }

        private void ThreadProcSafe()
        {
            //this.SetText("This text was set safely.");
            SetText("This text was set safely.");
        }

        public void SetText(string text)
        {
            if (this.ClientTextBox.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else

            {
                ClientTextBox.AppendText(text + "\n");
            }
        }
        public static void test(string mm)
        {
            //I can’t access SetText from here
        }
    }
    class TestInsert
    {
        public static void InsertTest()
        {
            Form1.test("test");
            //I can’t access SetText from here
        }
    }
}


PARTNERS