• Advertisement

• ### Popular Now

• 15
• 15
• 11
• 9
• 10
• Advertisement
• ### Similar Content

• By suliman
Hi!
I have a two floats (pan.x and pan.y) which allows the player to move the view in my tile-based 2d-gameworld. I also have a "float scale" for zooming the world in and out.
For drawing most stuff it works fine. I just (where pos is the tile-position of something and #define TILE 20 is the size of a gametile at scale = 1):
drawStuff(pos.x * scale * TILE, pos.y * scale * TILE); When I change scale it draws to the correct position on screen. But when i draw the lines that shows the grid (all the tiles) they do not offset correctly. It works when scale = 1, but when I lower scale they become off. I tried so many variations but I've got stuck. What am I doing wrong?
int tile2 = TILE * scale; int offY = ((int)pan.y) % tile2; // draw horizontal lines for the grid for (int i = 0; i < h; i++) gfx.lineRelative(0, TILE*i*scale + offY, gfx.screenX, 0, col);

• Trying to get ideas here on how I could do this:
I have created a game for a university coursework assignment using a custom game engine (internal to my university, but it's based on OpenGL).
The game loads levels and menus from a file, this happens at initialization to increase performance while playing the game. At the moment the game loading time is 4.1s, but the loading time without the levels and menus is 0.961s- that's over 3s. As more levels are added the loading time will become longer and I want to add a loading screen.
I've tried to add one, but loading the levels and menus is blocking everything else, including rendering the loading screen. This means that the loading screen doesn't appear until the game has loaded and is overridden by the main menu. The game loading is done via its update function and via calling initialization functions of the various game classes, and the loading screen is rendered within the game's render function (the rendering is done on a separate thread, according to the game engine's documentation).
Any suggestions on how I can show the loading screen without the other loading activity blocking it from being shown?
I know that I can use threading but am unsure how thread-safe the operation would be to init the game on another thread.

• I have a particle system with the following layout:
system / emitter / particle

particle is driven by particle data, which contains a range of over lifetime properties, where some can be random between two values or even two curves. to maintain a smooth evaluation between two ranges, i randomize a "lerp offset" on particle init and use that value when evaluating curves. the issue is that i'm using that same offset value for all properties (10ish) and as a result i'm seeing some patterns, which i'd like to remove. The obvious way is to just add more storage for floats, but i'd like to avoid that. The other way is to generate a seed of some sort and a random table, and use that to generate 10 values, ie: start with short/integer, mask it, then renormalize to float 0-1.

any other ideas?
• By dgi
Hey all ,
For a few days I'm trying to solve some problems with my engine's memory management.Basically what is have is a custom heap with pre allocated memory.Every block has a header and so on.I decided to leave it like that(not cache friendly) because my model is that every block will be large and I will have a pool allocators and stack allocators dealing with those blocks internally. So far so good I figure out how to place my per scene resources . There is one thing that I really don't know how to do and thats dealing with containers.What kind of allocation strategy to use here.
If I use vector for my scene objects(entities , cameras , particle emitters .. ) I will fragment my custom heap if I do it in a standard way , adding and removing objects will cause a lot of reallocations . If I use a linked list this will not fragment the memory but it's not cache friendly.I guess if a reserve large amount of memory for those vectors it will work but then I will waste a lot memory.I was thinking for some sort of mix between a vector and a linked list , where you have block of memory that can contain lets say 40 items and if you go over that number a new one will be created and re location of the data would not be needed.There would be some cache misses but it will reduce the fragmentation.

How you guys deal with that ? Do you just reserve a lot data ?

dgi
• By Hermetix
I am trying to setup the custom wizard for making a 3ds MAX 2018 plug-in (to export a character animation data), but I can't locate the wizard file folder to put the .vsz file in. In the 3ds MAX 2018 docs, it only mentions where the folder is in VS 2015 (VC/vcprojects). It's a VC++ project, but I don't see any folder in VC for the wizard files. I'm using VS 2017 update 15.5.6 Enterprise, and the folders in VC are: Auxiliary, Redist and Tools.

Thanks.
• Advertisement
• Advertisement

# C++ Pointer becomes invalid for unknown reason

## Recommended Posts

I've restructured some of my code to use namespaces and started getting problems in a module that was working correctly previously. The one in question is a DebugWindow, what happens is I give it a pointer to a variable that I want to monitor/change and it's job is to display that variable in a separate window along with a some + and - buttons to in/decrement the variable.

These are the relevant portions:

WindowManager.h

namespace WindowManager
{

/* WindowManager functions snipped */

namespace DebugWindow
{
void AddView(double* vard, std::wstring desc, double increment);
void AddView(std::wstring* vars, std::wstring desc);
void CreateDebugWindow(int width, int height, int x, int y);

}

}

Application.cpp is the main app, it calls the above functions to set the watch on the variables I need to see in real-time

void ApplicationInitialization()
{

//create the main window
UINT windowID = SR::WindowManager::CreateNewWindow(LocalWindowsSettings);

//initialize the rasterizer
InitializeSoftwareRasterizer(SR::WindowManager::GetWindow(windowID));

//create the debug window
SR::WindowManager::DebugWindow::CreateDebugWindow(400, LocalWindowsSettings.clientHeight, LocalWindowsSettings.clientPosition.x + LocalWindowsSettings.clientWidth, LocalWindowsSettings.clientPosition.y);

//display some debug info
SR::WindowManager::DebugWindow::AddView((double*)&gMouseX,TEXT("Mouse X"), 1);
SR::WindowManager::DebugWindow::AddView((double*)&gMouseY, TEXT("Mouse Y"), 1);

}

The variables gMouseX and Y are globals in my application, they are updated inside the App's WindProc inside the WM_MOUSEMOVE like so :


case WM_MOUSEMOVE:
{
gMouseX = GET_X_LPARAM(lParam);
gMouseY = GET_Y_LPARAM(lParam);

/* .... */

}break;

Now inside the AddView() function that I'm calling to set the watch on the variable

	void AddView(double* vard, std::wstring desc, double increment)
{
_var v;
v.vard = vard; // used when variable is a number
v.vars = nullptr; // used when varialbe is a string (in this case it's not)
v.desc = desc;
v.increment = increment;

mAddVariable(v);

}

_var is just a structure I use to pass the variable definition and annotation inside the module, it's defined as such

	struct _var
{
double* vard;       //use when variable is a number
double increment;   //value to increment/decrement in live-view
std::wstring* vars; //use when variable is a string
std::wstring desc;  //description to be displayed next to the variable

int minusControlID;
int plusControlID;

HWND viewControlEdit; //WinAPI windows associated with the display, TextEdit, and two buttons (P) for plus and (M) for minus.
HWND viewControlBtnM;
HWND viewControlBtnP;

};

So after I call AddView it formats this structure and passes it on to mAddVariable(_var), here it is:

void mAddVariable(_var variable)
{

//destroy and recreate a timer
KillTimer(mDebugOutWindow, 1);
SetTimer(mDebugOutWindow, 1, 10, (TIMERPROC)NULL);

//convert the variable into readable string if it's a number
std::wstring varString;
if (variable.vard)
varString = std::to_wstring(*variable.vard);
else
varString = *variable.vars;

//create all the controls
variable.viewControlEdit = CreateWindow(/*...*/); //text field control

variable.minusControlID = (mVariables.size() - 1) * 2 + 1;
variable.viewControlBtnM = CreateWindow(/*...*/); //minus button control

variable.plusControlID = (mVariables.size() - 1) * 2 + 2;
variable.viewControlBtnP = CreateWindow(/*...*/); //plus button control

mVariables.push_back(variable);
}

I then update the variable using a timer inside the DebugWindow msgproc

case WM_TIMER:
{
switch (wParam)
{

case 1: // 1 is the id of the timer
{
for (_var v : mVariables)
{
SetWindowText(v.viewControlEdit, std::to_wstring(*v.vard).c_str());
}

}break;

default: break;

}

}; break;

When I examine the mVariables, their vard* is something like 1.48237482E-33#DEN. Why does this happen?

Also to note is that I'm programming in C like fashion, without using any objects at all. The module consists of .h and .cpp file, whatever I expose in .h is public, if a function is only exposed in .cpp it's private . So even though I precede some functions with m_Function it's not in a member of a class but just means that it's not exposed in the header file, so it's only visible within this module.

Thanks.

Edited by VanillaSnake21

#### Share this post

##### Share on other sites
Advertisement

Code like

(double*)&gMouseX

isn't converting the value of gMouseX from an integer (which I'm assuming it is) to a double. It's converting the address of an integer to an address of a double. The code you give that address to assumes the address points to a double, and treats it as such. However, the bit patterns of the number "43" as a double are very different from the bitpatterns of the number "43" as an integer. Thus you are likely to get bogus results when you try to interpret them the way you're doing.

43, as an integer, has a 64-bit bitpattern of 00101011 (higher bits are all zero).

43.0, a double, has a 64-bit bitpattern of 01000000 01000101 10000000 00000000 00000000 00000000 00000000 00000000.

The bitpattern 00101011 as a double is 2.12448227711736013995924580933E-322.

(these values based on some random double bit calculator I found on the internet; accuracy not guaranteed, but the point is that they are very different representations)

In order for this technique of yours to work you're going to need to add support for each actual type of variable you're going to support (not just conceptual types like "number" and "string").

#### Share this post

##### Share on other sites

That did it. Thanks jpetrie. I totally forgot about the difference in bit layouts

## 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

• Advertisement