Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 24 Apr 2010
Offline Last Active Private

#5301001 Collision Meshes / Aabb Generation

Posted by Khatharr on 16 July 2016 - 02:23 PM

AABBs are for broad phase detection. It sounds like you're trying to use them for narrow phase.



#5300946 Questions About Storing Opcodes With Operands

Posted by Khatharr on 15 July 2016 - 11:08 PM

In x86 the registers are


general purpose registers:

EAX - free to use, holds function return values

EBX - free to use

ECX - free to use

EDX - free to use

these are 32 bit registers, but can be referred to as 16 bits AX,BX,CX,DX or 8 bits AH/AL/BH/BL/CH/CL/DH/DL


pointer registers:

ESI - source index, which is paired with...

EDI - destination index - these two are used for rapidly advancing pointer values when performing work that reads from one buffer and writes to another

EBP - the base pointer - points to the base of the stack frame, convenient for referring to function arguments or local variables, but not really required since the compiler can work out the math from...

ESP - the stack pointer - points to the top of the stack

EIP - index pointer - holds the address of the next instruction to be executed (after the currently executing one), can't be mov'd to but jmp or call can set its value


segment registers (used by virtual memory model):

CS - code segment - pointer to page that holds the unpacked exe file (or whatever. the bytecode is in this page)

DS - data segment - pointer to page that holds the heap

SS - stack segment - pointer to page that holds the stack

ES - for an extra page

FS - for an extra page

GS - for an extra page


flags register:

EFLAGS - a register used to store binary flags set by various common operations - allows for basic branching logic by remembering the results of comparisons, etc. For example, when you use greater than/less than in C you're setting the SIGN flag, and a conditional jump statement like JLE (jump if less than or equal to) will read the sign flag when deciding whether to jump, so the statement:


if(x < 2) {





could theoretically be compiled as:


CMP 2,x //compare 2 to x - this performs an "implicit subtraction", that is it executes "2 - x" but does not store the result in a register, though it does set the sign bit and zero bit in EFLAGS according to the result of the subtraction.

JLE B //if 2 was less than or equal to x then jump ahead to B, otherwise just keep going

A //compiled code of A

B //compiled code of B


There are also usually sets of special registers depending on the feature set of the architecture. (usually things like SSE or MMX)


Old instruction set reference and general system explanation: http://css.csail.mit.edu/6.858/2015/readings/i386.pdf

Very old discussion of exe format: https://msdn.microsoft.com/en-us/library/ms809762.aspx


Modern resource (64 bit architecture): http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html

#5300810 Please Help Stuck On What To Do Here (C++)

Posted by Khatharr on 14 July 2016 - 06:34 PM

This code will not compile. I see several spelling and syntax errors just at a casual glace. Before pursuing the exercise how about correcting the code that you have here and ensuring that your additions work correctly? (hint - the additions are not correct) For exercises like this you really ought to be working in some form of development environment where you're regularly compiling and checking your code. If you don't have one then here are some things you can use:


VS2015 (Microsoft's Windows IDE): https://www.visualstudio.com/en-us/visual-studio-homepage-vs.aspx (click on "Download Community 2015")

ideOne (online compiler - runs in browser): https://ideone.com/


Further, this seems like homework, especially since I can't imagine any other reason to reach this point without being able to at least venture a guess at how to implement this feature. If you understand how the linked list works (which is probably the objective of the section) then you really should have no difficulty with this whatsoever.


Edit: Yeah. I knew this code was familiar. This is from Beginning C++ Through Game Programming. Read through the section again (starting from "Introducing the Game Lobby Program") and get clear on how the system works. Once you've done that, remember that the container (the lobby) starts out empty and you add and remove elements (players) one at a time. You should be able to reason about how to keep a tail pointer up-to-date here. If not then you need to continue to review the program until you understand how it works.


I think it would be doing you a grave disservice to simply answer this question directly. Those questions exist to ensure that you understand the material.

#5300766 Questions About Storing Opcodes With Operands

Posted by Khatharr on 14 July 2016 - 12:43 PM

umm I really don't know what purpose the call stack is. is that true the call stack memory is in RAM and CPU provides stack operations?



Yes and yes. The call stack is pushed when you enter a function and it contains that function's local variables. The 'stack frame' for that function also includes some data about where the function was called from so that when the function returns the stack is popped and it has the information about where to go to in the bytecode in order to return to the calling function.


If you compile some simple, minimal functions in Visual Studio you can put a breakpoint in the program and run it, then when the debugger stops at the breakpoint you can right-click on the code and 'View disassembly' to get a look at the assembly that pushes and pops the stack (the instructions in x86 are 'call' and 'ret'). There's an option to also view the compiled bytecode for each instruction.


You can also add registers to the variable watch list, and view the call stack in the memory viewer. If you take things one instruction at a time you should be able to work out what it's doing. Just note that the stack 'grows' backward in memory. That is, it starts at a higher address and when it's pushed to the new data goes into lower addresses.



#5300762 Newbie Question About Std:vector

Posted by Khatharr on 14 July 2016 - 12:17 PM

That 'initialize' is making my teeth grind.


drwhat, do you know about member initialization lists?

#5300599 c++: replace word in char array?

Posted by Khatharr on 13 July 2016 - 02:33 PM

But using only char array (no string class or helper stuff).

By the pricking of my thumbs, buffer overflows - here they comes.

#5300598 How to automate texture deleting?

Posted by Khatharr on 13 July 2016 - 02:30 PM

I don't see any problem using std::unordered_map<filename_string, texture> for storing textures. They're not something you're going to be iterating often, so it's not really a performance concern. One approach is to load resources this way as a cache. That is, have a loader encapsulated in a 'Cache' class with an unordered_map. When the user requests a resource from the cache, it looks to see if the map already holds the resource, and if not it loads the resource into the map. Then it just returns the mapped resource and you avoid reloading things you've already loaded.

#5300226 Object not rotating as it should

Posted by Khatharr on 11 July 2016 - 12:40 PM

I don't know where the origin is in that model, but if you want to rotate around its center you need to translate it such that the origin is at that point, then perform your rotation, then translate it again to where you want it to be. If you're already aware of this then try changing your argument order in the multiplications. The Matrix.multiplyMM() documentation states that the args are in non-intuitive order.

#5300014 Finding that balance between optimization and legibility.

Posted by Khatharr on 10 July 2016 - 12:54 PM

Number of times code will be executed: 100,000

Time saved per execution: 3ms

Total time saved: 300 seconds

Time spent with hand in shorts deciding how to save the most time: 1 hour

#5299899 A good way to handle managing bullets/projectiles

Posted by Khatharr on 09 July 2016 - 01:43 PM

Just use a vector of structs and work on the vector. You can do this through a function, or else encapsulate the vector in a class that manages it and does the work on it. When you iterate the vector to update the bullets and you find an object that's flagged as dead then swap and pop and continue processing from that point. If a new bullet is fired, just emplace it to the vector. Vector retains its memory and you'll avoid the costs that you'd otherwise get from constant allocations and releases. The only thing you need to make sure of is that you never increase the vector during iteration. In fact, if you like, you can set up your system to log the maximum size that the vector reaches during play and when you're satisfied that you've found a good upper-bound you can use vector.reserve() to allocate all the memory up-front (increase it by 50% just for kicks).


You're going to have a hard time finding a better pattern than this, and we're talking about something that almost certainly is not a performance issue in the first place.

#5299897 Does Object Pooling with a Vector in C++ have problems with memory?

Posted by Khatharr on 09 July 2016 - 01:21 PM

This has become morbidly fascinating.

#5299731 Does Object Pooling with a Vector in C++ have problems with memory?

Posted by Khatharr on 07 July 2016 - 10:31 PM

Regarding std::vector's use of memory:

#include <vector>
#include <iostream>

void showVectorStats(const std::vector<int>& vec) {
  std::cout << "Size: " << vec.size() << ";\tCapacity: " << vec.capacity() << "\n";

int main() {
  std::vector<int> derp;

  std::cout << "Created vector.     > ";

  std::cout << "\n";

  while(derp.size() < 10) {
    std::cout << "Pushed one element. > ";

  std::cout << "\n";

  while(!derp.empty()) {
    std::cout << "Popped one element. > ";


Please run this and consider the results displayed.

#5299679 Does Object Pooling with a Vector in C++ have problems with memory?

Posted by Khatharr on 07 July 2016 - 02:50 PM

These result in the same initialization, but the last one is the most efficient.

BulletPool(Bullet, int num)
  mCounter = num;
  int i = num;
  while(--i > -1)
BulletPool(Bullet, int num)
  mCounter = num;
  for(int i = 0; i < num; i++) {
BulletPool(Bullet, int num) :


I think that this is not the pattern that you want.

Bullet GetBullet() //this is returning by value - it will make a copy, not a reference
  if(mCounter > 0)
    return mBullets[--mCounter];
    std::cout << "Error: You exhausted the pool!" << '\n'; //this is an error, but

  return mBullets[mCounter]; //you go from there to here, returning an object that's already in use


Maybe something more like this:

bool GetBullet(Bullet** ppBullet) {
  if(mCounter > 0) {
    *ppBullet = &mBullets[--mCounter];
    return true;

  return false;


However, the real issue here is the overall approach to the problem: Why are you trying to make a pool like this? You could easily just use a vector and reserve, or even just use a std::array, but this checkout/return idea is not going to work for you. What problem are you trying to solve? What's the goal of this class?

#5298954 Making an object move back and forth.

Posted by Khatharr on 03 July 2016 - 09:45 PM

That said, the steps I outlined should be fairly clear. Have you tried following them? If so, which step are you having trouble with?

(Did you miss that initial exchange up top?)

#5298830 dynamic list

Posted by Khatharr on 02 July 2016 - 04:03 PM

You don't need to keep the objects themselves sorted; you only need to sort their order in the display. I also thought of a priority_queue, but that doesn't support iteration. Maybe if you used it on a set of pairs with pointers and priority levels and just rebuilt it and emptied it on each frame. It uses vector for internal storage by default, so if you re-use the container it should cut down on the allocation cost.


Than again, maybe a 'bucket' approach would be better here. Instead of storing each item's priority (color) as a member of the object, just have a vector for each priority level. Place the objects in the vector that matches their priority and then just iterate the vectors in the order you want.


Would that work?