• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Renthalkx97

Possibly a better design for rendering?

5 posts in this topic

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.

Edited by Swiss Premium Iced Tea
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
@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?
0

Share this post


Link to post
Share on other sites


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.)

0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

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).

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0