Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 15 Jun 2007
Offline Last Active Jan 28 2016 12:25 PM

#5193879 Proper C++ header file?

Posted by on 20 November 2014 - 05:01 PM

I would also suggest to replace







#pragma once


it's simpler and you don't need the #endif at the end :)

#5163696 How do multyple people write code for one project?

Posted by on 29 June 2014 - 02:27 PM

Use something like github, it will make you're life much easier. Although i cannot give you much advice since i only used it for my personal project, never coded in a team.

#5163462 speeding this with sse or sse intrinsics

Posted by on 28 June 2014 - 10:18 AM

Why do i even bother...

#5163455 My game's beta is finished but it won't run on other comptuers

Posted by on 28 June 2014 - 09:50 AM

My suggestion is learn to use the debugger and find why your character start at this odd place. Using a debugger is really not that hard.

#5163454 speeding this with sse or sse intrinsics

Posted by on 28 June 2014 - 09:42 AM


Keep in mind that your code is not hardware accelerated so you wont get much better perfs than that.

Also, your dump show that your compiler IS using sse optimizations (no y there...)


#5163310 decompositing and recompositing color (pixel)

Posted by on 27 June 2014 - 03:44 PM

As other pointed out to you many times, just stop worrying about micro-optimization like that, today, it's almost impossible to beat the compiler optimizations in release builds. I tried it (something similar to this, color packing/unpacking), with MMX and SSE, i could beat a debug build, but not a release build (it was a tie), because you know what, the compiler(visual studio) in release build use SSE optimizations when it can so i basically did that test for nothing, except learning that such asm optimization are worthless now, in most cases.


With that said, it's always usefull to do some profiling to find the real bottlenecks and optimize the algorithms where it count.

#5162317 Searching txt files

Posted by on 23 June 2014 - 07:18 AM

You could use fscanf i beleive, something like this:



char c;
int x, y, z;

char szString[256];
ZeroMemory(szString, 256);

FILE *f = fopen("YourFileName.txt", "rt");

// add error checking later...
fscanf(f, "%c = %d-%d\n", c, x, y);
fnscanf(f, 256, "%s\n", szString);
fscanf(f, "%d-%d %d\n", x, y, z);
fscanf(f, "%d-%d %d\n", x, y, z);
fscanf(f, "%d-%d %d\n", x, y, z);
fscanf(f, "%d-%d %d\n", x, y, z);
fscanf(f, "\n");



#5161951 ANSI C - Custom 2D level array

Posted by on 21 June 2014 - 12:51 PM

The best part about that engine was making levels, the worst part was everything else lol, so that's why I moved to C.


I don't get it. You can still do almost everything in c++ as you are doing in c, but at least c++ give you classes, new, delete, ect

Most peoples refer to this as "C with classes".


Your life will be a lot easier if you learn oop properly.




int NumCells = width * height;
int *level = malloc(NumCells * sizeof(int));
brings the same results as:
level = malloc(sizeof(int) * width * height);


I know, i added it for readability, and in case you might need it later in the code for example.


And, for your little array problem, you have a map of 20x13 tiles, that make 260 tiles. Now, each tiles take 4 bytes (1 int) to represent so that's 260x4 = 1040 Bytes.

As long as you pass a value between 0 and 259 inclusive in the level[] bracket, you're fine. If not, your code have a bug because it read into junk memory.


If you want to represent the first tile with 1 instead of zero, you have to subtract one from the x and y in the (y*h)+x formula.



for (y = 1; y <= height; y++){

    for (y = 1; y <= height; y++){

        level[((y-1)*height) + (x-1)]=lua_tonumber(L, -1);




C++ array are zero based, lua might start at one so that's why you're having bugs, so, for everything lua, you use 1 to x, in c/c++ use 0 to x-1.


My class might look cool but that's not it's purpose, it's purpose is to keep your code safe and organized.

CLevelManager LevelManager;


// get a cell value
int val = LevelManager.GetCell(x-1, y-1);

// Set a cell value (this should not have to be called here, but in the LevelManager.Load() function instead)
LevelManager.SetCell(x-1, y-1, lua_tonumber(L, -1));

That's much more readable than a big function imo. The getter and setter prevent you from reading/writting to garbage memory, and if you forgot to release the memory allocated, the destructor have your back.


Same thing for the lua code, encapsulate that so it's easier to work with, that's what i did.

#5161935 ANSI C - Custom 2D level array

Posted by on 21 June 2014 - 10:23 AM

level = malloc(width * height);


Keep in mind that you are allocating ints, not byte, so you have to adjust for that:


int NumCells = width * height;

int *level = malloc(NumCells * sizeof(int));


for (y = 1; y <= height; y++) {
for (x = 1; x <= width; x++) {


this is bad too, array are zero based in c++, so you're off by one here, so, that's another bug. Try this instead:

for (y = 0; y < height; y++) {
    for (x = 0; x < width; x++) {
        int Indx = (y * width) + x;
        level[Indx]=lua_tonumber(L, -1);

The reason that it crash is

a) The buffer is too small

b) you forgot to substract 1 from x and y in the loop


If you want to move the array outside the function, i suggest making a class, for example CLevelManager then store everything level related there. It would make a lot more sense that way and it would be easier to remember to free the allocated memory you new'ed in the destructor,



class CLevelManager {
    int *Cells;
    int Width;
    int Height;

    void AllocateCells(int w, int h);
    void FreeCells();

    int  CalcOffset(int x, int y);
    bool IsCellValid(int x, int y);
    void LoadLevel(char *fname);
    void FreeLevel();
    int  GetCell(int x, int y);
    void SetCell(int x, int y, int value);

    int GetWidth(){return Width;}
    int GetHeight(){return Height;}

    Cells = NULL;
    Width = Height = 0;


void CLevelManager::AllocateCells(int w, int h)
    // Free previous cells, if any

    // Calculate the buffer size
    int BufSize = w * h * sizeof(int);
    // Allocate the cells
    Cells = malloc(BufSize);
    // Initialize them
    ZeroMemory(Cells, BufSize)

void CLevelManager::FreeCells()
        Cells = NULL;

void CLevelManager::LoadLevel(char *fname)
    // put your loading routine here
    // Width  = ;
    // Height = ;

    // AllocateCells(Width, Height);

    // set the cells here (loop)

void CLevelManager::FreeLevel()

bool CLevelManager::IsCellValid(int x, int y)
    if(!Cells || x >= Width || y >= Height)
        return false;

    return true;

int CLevelManager::CalcOffset(int x, int y)
    return (y * Width) + x);

int CLevelManager::GetCell(int x, int y)
    if(!IsCellValid(x, y))
        return -1;
    return Cells[CalcOffset(x, y)];

void CLevelManager::SetCell(int x, int y, int value)
    if(!IsCellValid(x, y))

    Cells[CalcOffset(x, y)] = value;

So, i coded this in 5 mins so please, don't yell at me if there's any bugs smile.png(it's just a draft), It should give you an idea about how you could wrap this up in it's own class.


EDIT: just added a few more lines, that should be enough to get you started.

#5157626 Does C++ have a squared operator?

Posted by on 02 June 2014 - 02:02 PM

Well, for one it's extremely simple, compared to your method, and two, it's really fast since it's dosn't involve invoking a function, but it's just a sugestion after all, the op can choose whatever method he prefer, i was just pointing it out.

#5157136 Seems like I can't do anything without tutorials

Posted by on 31 May 2014 - 07:54 AM

Posted Today, 05:54 AM

I don't bother storing anything much in my head. There is too much information. I've been using things like Win32, DX and Qt for many years now, but I'm still constantly running to the docs or to sample code. There's nothing wrong with this. The skill to develop isn't memorizing APIs, its being able to quickly find out the answers to questions as you need to.

Understanding the concepts behind algorithms and being able to translate a real-world requirement into the relevant programming concepts is not something that requires a huge amount of stored information in your head either.

So don't feel bad. API docs and sample code are far better tools than human memory (mine at least) for this kind of thing.


I Agree with this, learning a language is good, learning apis, no so much. Docs here there for that reason.

#5156187 Finding the nearest point to a shape.

Posted by on 26 May 2014 - 09:50 PM

If all you need is to calculate the distance between one of the box corner to the sprite center, just use the distance formula.




Do this for each 4 points of the box and the shortest value will be the nearest one.

#5155820 Embedded Python 3.3 problem...

Posted by on 25 May 2014 - 03:34 AM

I know this post is really old now, but i just wanted to add the solution i just found in case that could help someone in a similar situation. The problem was that python use the registry to know where to find it's library (you don't just need the dll you also need the python files), and for a machine with no python installed it search in directories relative to the application directory, so that's why it crashed/failed ect.


The solution i've found is to call PySys_SetPath() with the 3 required directory: "<python dir>/DLLs" "<python dir>/Lib" and "<python dir>/Lib/lib-tk"


<python dir> can be a directory or even a .zip file.


For example, if your program is in "C:\MyApp", the value to pass to PySys_SetPath() should be






So, using the poorly written but working code, all works

char* ExtractFilePath(char *fullpath)
	static char Path[MAX_PATH];
	ZeroMemory(Path, MAX_PATH);

	int Pos = -1;
	int Size = strlen(fullpath);

	for(int i = Size-1; i >= 0; i--){
		if(fullpath[i] == '\\'){
			Pos = i;

	if(Pos > 0)
		memcpy(Path, fullpath, Pos); 

	return Path;

// Set paths to directories
/*void SetPythonPaths(char *AppDir)
	LPCSTR DllsDir = "\\Python27\\DLLs";
	LPCSTR LibDir = "\\Python27\\Lib";
	LPCSTR libtkDir = "\\Python27\\Lib\\lib-tk";

	int PathBufSize = (strlen(AppDir) * 3) + strlen(DllsDir) + strlen(LibDir) + strlen(libtkDir) + 3;
	char *PythonPaths = new char[PathBufSize];
	ZeroMemory(PythonPaths, PathBufSize);
	sprintf(PythonPaths, "%s%s;%s%s;%s%s", AppDir, DllsDir, AppDir, LibDir, AppDir, libtkDir);
	delete [] PythonPaths;

// Set paths to .zip file
void SetPythonPaths(char *AppDir)
	LPCSTR DllsDir = "/DLLs";
	LPCSTR LibDir = "/Lib";
	LPCSTR libtkDir = "/Lib/lib-tk";
	LPCSTR ZipName = "\\Python27.zip";

	int PathBufSize = (strlen(AppDir) * 3) + (strlen(ZipName) * 3) + strlen(DllsDir) + strlen(LibDir) + strlen(libtkDir) + 3;
	char *PythonPaths = new char[PathBufSize];
	ZeroMemory(PythonPaths, PathBufSize);
	sprintf(PythonPaths, "%s%s%s;%s%s%s;%s%s%s", AppDir, ZipName, DllsDir, AppDir, ZipName, LibDir, AppDir, ZipName, libtkDir);
	delete [] PythonPaths;

void CPythonScript::InitPython()

		module = PyImport_AddModule("__main__");
		dictionary = PyModule_GetDict(module);


		Initialized = true;

Also, i had to add Py_NoSiteFlag=1; to the init function for some reason.

#5154106 References and Environmental Variables

Posted by on 16 May 2014 - 02:30 PM

Which folder exactly? The one with the .sln, .suo and .sdf files, or the one with the .vcxproj, .filters and .users files?


Im not exactly sure since i generally uncheck the "create directory for solution" box when i create new projects. I think you can find it by going to the project configuration dialog, in project->project property->configuration property->debugging->working directory. You should see $(ProjectDir) in the box, click the browse... option and you should see what directory it use.


When deploying the program, it's a lot simpler, just drop the dlls in the same folder as the executable, it will try to find them there first. If it can't find them it will check the windows and windows/system32 directory, then the environement variables.

#5153603 Loading Large Number of Files

Posted by on 14 May 2014 - 11:04 AM

1,333 images for 1 character ?!? Seriously? As serial kicked said, i think it would be best to load them as a single file, all images in one file, then store texture UV for each sprite somewhere and use that so you don't need to reload/change textures continuously.