Possibly a better design for rendering?

Started by
4 comments, last by Fredericvo 9 years, 7 months ago

Hey! So I'm writing a game engine and I'm running into problems. :/ Keep in mind it's almost 5:30 AM and I'm probably not thinking logically. So if the solution is obvious please forgive me for asking. tongue.png Anyways, I have a Graphics, Renderer, ObjectManager, and WorldObject class that all work together to render the graphics(just sprites currently). The Graphics sets up DirectX and has BeginFrame() and EndFrame() functions. The Renderer class uses those two functions before(BeginFrame()) and after(EndFrame()) it's Render() function which iterates over a std::map of elements of type WorldObject. Then finally I have my ObjectManager class that I use to manage the aforementioned std::map.

Now here's where things get not too good. D3DXCreateTextureFromFile() and D3DXCREATESPRITE() both have a LPDIRECT3DDEVICE9 as of its parameters. Renderer is a friend of Graphics. So what I did is in the constructor of Renderer I iterated over the std::map and was able to call those two functions for each texture/sprite. My Renderer shouldn't be creating textures/sprites though. Its job is to render things. Also I have a LPDIRECT3DTEXTURE9 and LPD3DXSPRITE, among others, in my WorldObject class because the derived objects use them when I add an object to the std::map. Why not just make your LPDIRECT3DDEVICE9 accessible to your WorldObject so each derived object can create the texture/sprite itself? That just seems sloppy to me becuase I don't think objects in the game need to know about DirectX related stuff. They should know their position, state, logical data, etc. So what I'm asking is how can I improve on this? I already scrapped my last project because once it got so big everything started falling apart, so I'm trying to do this one properly.

Here's some source code:


#include "ObjectManager.h"
#include "Player.h"

ObjectManager::ObjectManager()
{
	AddObject(new Player("Player.png", 100.0f, 100.0f), "Player1");
}

ObjectManager::~ObjectManager()
{
	for(auto i : m_WorldObjects)
	{
		delete i.second;
		i.second = nullptr;
	}

	m_WorldObjects.clear();
}

void ObjectManager::AddObject(WorldObject *Object, std::string objectID)
{
	m_WorldObjects.insert(std::pair<std::string, WorldObject*>(objectID, Object));
}

std::map<std::string, WorldObject*> ObjectManager::m_WorldObjects;

#include "Renderer.h"

Renderer::Renderer(Graphics &Graphics)
:
m_Graphics(&Graphics)
{
	for(auto i : m_ObjectManager.m_WorldObjects)
	{
		if(i.second->m_renderable && i.second->m_visible)
		{
			D3DXCreateTextureFromFile(m_Graphics->m_d3dDevice9, i.second->m_textureFileName.c_str(), &i.second->m_d3dTexture);
			D3DXCreateSprite(m_Graphics->m_d3dDevice9, &i.second->m_d3dSprite);
		}
	}
}

bool Renderer::Update()
{
	if(!m_Graphics->BeginFrame())
	{
		return false;
	}

	Render();

	if(!m_Graphics->EndFrame())
	{
		return false;
	}

	return true;
}

void Renderer::Render()
{
	for(auto i : m_ObjectManager.m_WorldObjects)
	{
		if(i.second->m_renderable && i.second->m_visible)
		{
			i.second->m_d3dSprite->Begin(0);
			
			i.second->m_d3dSprite->Draw(i.second->m_d3dTexture, nullptr, nullptr, &i.second->m_position, D3DCOLOR_XRGB(255, 255, 255));

			i.second->m_d3dSprite->End();
		}
	}
}

#pragma once

#include <d3dx9.h>

class WorldObject
{
public:
	bool m_renderable, m_visible;

public:
	LPDIRECT3DTEXTURE9 m_d3dTexture;
	LPD3DXSPRITE m_d3dSprite;
	D3DXVECTOR3 m_position;
	std::string m_textureFileName;
};

Thank you in advance for any help.

Love,

Swiss Premium Iced Tea<3

EDIT:

So I think I solved my problem. I haven't tried implimenting it yet, but I think it'll work. I can add two functions to my Graphics class: LoadTextureFromFile(std::string, LPDIRECT3DTEXTURE9 d3dTexture) and CreateSpriteFromTexture(LPD3DXSPRITE d3dSprite, LPDIRECT3DTEXTURE9 d3dTexture). That way I don't need to let any other classes have access to the D3DDEVICE. But then where will I call them from? I don't want to call them from Renderer or WorldObject. -.- Maybe I need another class? I don't know.

Advertisement

First: you're making a lot of sprite->Begin/ ->End calls where only one of each call is needed. That is, you can create a single sprite, begin rendering with sprite->Begin, call sprite->Draw with each object's data in a loop, and then call sprite->Flush or sprite->End.

Second: there appears to be no need for Renderer to call BeginFrame and EndFrame. The graphics object can do that and then call the Renderer.

You can further separate a couple of tasks - creating/loading data for a sprite can be separate from managing them. That is, consider separating "sprite-maker" and manager. E.g., the Graphics class can create a sprite-maker when needed, load some data, and delete the sprite-maker. Creating a sprite is a transient need.

Sprites can all be rendered with a single sprite object. The Renderer can be just a wrapped ID3DXSprite. You might then consider an object class (or simple structure) that is merely a container for the data needed to render a sprite.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

@Buckeye

Yes, I'm aware that I only need to call Begin/End once per batch of sprites rendered. Having the LPD3DXSPRITE in the WorldObject class only allowed me to to access it through my map iterator, so it made more sense at the time being to just call them in the Renderer Update loop where the iterator was.

You're right, Renderer doesn't even need to know Graphics aside from the LPDIRECT3DDEVICE. I was just putting it with the Renderer so I didn't need to create a Graphics object inside my Game object. I'd just have the Renderer.

Lastly, a sprite factory sounds like a good idea. I'll try that out when I get back home. Should solve my problem.

Thank you for reply.

Also do you happen to be from Ohio?


I was just putting it with the Renderer so I didn't need to create a Graphics object inside my Game object. I'd just have the Renderer.

Do you really need anything more than the graphics class with a sprite to render with? Just sayin'.

(Yeah, I'm a native Buckeye.)

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Do you really need anything more than the graphics class with a sprite to render with? Just sayin'.


As of now, no. The sprite, I assume, can be used for everything 2D that I'd need to draw. Correct? Reason I added a Renderer is because I want to be able to eventually expand on this project(i.e. maybe different rendering modes or something along those lines?.) I've seen a lot of engines actually which have a Scene class which does all the actual rendering. So I guess I'm trying to replicate that.

And I'm in NE Ohio, in the Youngstown area.

Game Objects should not contain directx9 stuff. (what if you ever wanted to use OpenGL? Or DX 11?)

They should only contain position, velocity, game specific related stuff and... something like an int or enum that you will use to identify what model/sprite/texture etc to use (and which are stored in their respective manager classes)

So you'd iterate through your objects as now, retrieve their position and an an id for a sprite (a place on a spritesheet texture), then send the relevant info as parameters to your renderer's method(s).

This topic is closed to new replies.

Advertisement