# Bismuth

Member

54

105 Neutral

• Rank
Member
1. ## Copy array with variable remap

Hello. Is there an algorithm or a function to remap an array in C to another array with a constant maping? I.e. I have an array of bytes (uint8_t) with 4 elements, and I would like to copy the values from this array to another array with a "static remap". [CODE]uint8_t array_in[4]; uint8_t array_out[4]; void array_remap(uint8_t map, uint8_t * array_in, uint8_t * array_out) { ... }[/CODE] The function also takes a map parameter. This means that every time the function is called it has to remap the variables in the same order, provided that the map parameter is identical. Basically it acts like a seed number for the random remapper. Here's how the remap is supposed to take place according to the seed number. The remap table doesn't have to be exactly as displayed, but it should offer all 24 possibilities (4 elements mean 4! = 24 total combinations). [quote]0x00: abcd -> abcd 0x01: abcd -> abdc 0x02: abcd -> acbd 0x03: abcd -> acdb 0x04: abcd -> adbc 0x05: abcd -> adcb 0x06: abcd -> bacd 0x07: abcd -> badc 0x08: abcd -> bcad 0x09: abcd -> bcda 0x0A: abcd -> bdac 0x0B: abcd -> bdca 0x0C: abcd -> cabd 0x0D: abcd -> cadb 0x0E: abcd -> cbad 0x0F: abcd -> cbda 0x10: abcd -> cdab 0x11: abcd -> cdba 0x12: abcd -> dabc 0x13: abcd -> dacb 0x14: abcd -> dbac 0x15: abcd -> dbca 0x16: abcd -> dcab 0x17: abcd -> dcba[/quote] I could write the function by using a 4x4=16 bit field inside an uint16_t function parameter, but I'm trying to reduce the parameter size to a simple index number (8 bits). Could the map parameter be somehow reduced to fit inside a byte (8 bits)? Any ideas?
2. ## Integer multiplication, division

[quote name='Brother Bob' timestamp='1349815016' post='4988474'] Your variable a holds a negative value and you're casting it to an unsigned integer. Unsigned variables cannot hold negative values. And yes, it is technically enough to cast just a or b, because the other operands will be promoted automatically.[/quote] Eh, sorry it was a typo. I wonder why I typed uint anyway. int16_t cc = (int32_t)aa * bb / (pe->fade_out); // This works.
3. ## Integer multiplication, division

Thank you for explaining the situation, this clears up my doubts. I am programming an application in AVR Studio 5.1 using gcc for an Atmega328p microcontroller. Due to limited RAM on a microcontroller (2 KB) I tend to stick to 8-bit and 16-bit variables. I did some tests and it seems that increasing at least one of the vars in the equation will produce the correct result. This does not seem to work though. int16_t result = (uint32_t)a * b / c; // does not work I'll do some more tests. Regards, Bismuth
4. ## Integer multiplication, division

[quote name='Cornstalks' timestamp='1349812336' post='4988454'] Actually, you're running out of precision in that operation. -255 * 409 is -104,295, but [font=courier new,courier,monospace]int16_t[font=arial,helvetica,sans-serif] [/font][/font]only has 16 bits of storage, which means it can only store values in the range ?32,768 to 32,767. So what happens? Your intermediate result gets chomped down to 16 bits *before* the divide happens, and the end result is a weird value due to [url="http://en.wikipedia.org/wiki/Integer_overflow"]overflow[/url]. What should you do about it? Use a bigger data type. [/quote] Could you please elaborate on that? What exactly is the factor that causes the calculation to be processed in 16-bit? Shouldn't the program detect that its running out of precision and automatically use a bigger (i.e. 32-bit) temporary buffer? Which variables exactly should be increase in size? EDIT: Is there a way to increase the temporary buffer size without changing the variable data types?
5. ## Integer multiplication, division

But the final result -104 does not overflow the output buffer. It's clearly in the range, so it should be assigned no problem. I'm not sure I understand what's going on here.
6. ## Integer multiplication, division

[code]int16_t a = -255; int16_t b = 409; int16_t c = 1000; int16_t result = a * b / c;[/code] result: 26 expected: -104 What the heck is going on here?
7. ## Opinion: What should I name the generic handle type?

I vote for [b]handle[/b].
8. ## Custom keyword and object type flag (solved, code attached)

Yeah, I understand. I thought it was a relevant topic, so I linked it. The first post in that thread was only a preliminary suggestion. I thought I would put the feature in the core because it already has a parser that I would simply extend. What I forgot to suggest was to wrap the whole implementation into a #ifdef directive, so that a user could choose whether or not to have certain features included in the core library at compile time by simply adding lines like "#define AS_USER_KEYWORDS 1" to as_config.h ... kind of like how compiling a linux kernel works. Though I see you prefer to use addons for this purpose.
9. ## Custom keyword and object type flag (solved, code attached)

I suggested something like this already. Having a keyword defined right before a variable makes the code a lot more readable as opposed to having all keywords located at the top of a class definition. I also dislike the use of square brackets in front of variables to wrap some keywords. It just looks weird and non-C++ish. While the metadata approach does offer some flexibility, its rather cumbersome *imho*. You're also required to use two separate parsers (one for metadata, another for scripts), and thus two separate databases for keywords. For this reason I suggested my approach [url=http://www.gamedev.net/topic/591552-idea-user-defined-keywords/page__p__591552]here[/url].
10. ## [C/C++] Coding style dilemma

[b]landlocked[/b]: I'm not sure I understand. I do have only one function handling cleanup... cleanup for multiple API calls. I can't edit the API calls since they're from windows DLL's. [b]Serapth[/b]: I am not exactly opposed to them. I've just rarely ever used them. [b]SiCrane[/b]: If I create the three objects A, B and C in that order, what guarante do I have that they will be destroyed in the reverse order when they go out of scope? I should have probably posted this in my first post, here's the code I was talking about: [code] bool InitializeOpenGl(HINSTANCE hInstance) { bool result = false; WNDCLASSEX wc; memset(&wc, 0, sizeof(WNDCLASSEX)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_OWNDC; wc.lpfnWndProc = DummyWndProc; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.lpszClassName = L"DummyClass"; wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO); ATOM aWndClass = RegisterClassEx(&wc); if (aWndClass != 0) { HWND hWnd = CreateWindowEx( NULL, L"DummyClass", L"Dummy", WS_OVERLAPPEDWINDOW | WS_DISABLED | WS_ICONIC, 0, 0, 320, 240, NULL, NULL, hInstance, NULL ); if (hWnd != 0) { printf("Dummy window hWnd: 0x%X\n", hWnd); HDC hDC = GetDC(hWnd); if (hDC != 0) { printf("Dummy window hDC: 0x%X\n", hDC); PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; // Version Number pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; int nPixelFormat = ChoosePixelFormat(hDC, &pfd); if (nPixelFormat != 0) { printf("Pixel format for dummy window: %i\n", nPixelFormat); BOOL bResult = SetPixelFormat(hDC, nPixelFormat, &pfd); if (bResult != 0) { HGLRC temp_context = wglCreateContext(hDC); if (temp_context != 0) { wglMakeCurrent(hDC, temp_context); //glewExperimental=TRUE; GLenum err = glewInit(); if (err == GLEW_OK) { if (GLEW_VERSION_3_2) { // Final code here result = true; int major = 0; int minor = 0; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); printf("OpenGl version: %i.%i\n", major, minor); } else { printf("OpenGL 3.2 Not supported!!!\n"); } } else { printf("Cannot initialize GLEW.\n"); printf("%s\n", glewGetErrorString(err)); } // Cleanup the existing GL context wglMakeCurrent(NULL,NULL); wglDeleteContext(temp_context); } else { printf("Unable to create GL context\n"); } } else { printf("Unable to set pixel format for dummy window.\n"); return 0; } } else { printf("Unable to get pixel format.\n"); } // Release drawing context ReleaseDC(hWnd, hDC); } else { printf("Cannot access the device context.\n"); } // The window is created, we have to destroy it. DestroyWindow(hWnd); } else { printf("Cannot create window\n"); } // Since the window class has been registered by this point we have to unregister it. UnregisterClass(L"DummyClass", hInstance); } else { printf("Cannot register the class.\n"); } return result; }[/code]
11. ## [C/C++] Coding style dilemma

I have a self-inflicted dilemma about different coding styles. Suppose you have a list of functions/API's that need to be called in a particular order, as each one depends on the previous to succeed. We're also going to have to cleanup the resources allocated by these functions after we're finished. However, should one function fail, the whole routine fails, but we still have to bail out and free the resources that were allocated so far. If a function fails it returns zero, and non-zero otherwise. A typical example would be setting up an OpenGL 3.x context. In order to get a OpenGL 3.x context you first have to create a dummy OpenGL context, get some pointers, and then destroy it. This is, however, only an example, I am talking about general approach in this thread. Consider this function: [code]bool SomeFunction() { bool result = false; int a = DoSomething(); if (a != 0) { int b = DoSomethingElse(); if (b != 0) { int c = DoYetAnotherThing(); if (c != 0) { ... etc ... // Final code here result = true; ... etc ... // Because we know DoYetAnotherThing succeeded we have to free the // resources regardless of what happens in the nested code. CleanupYetAnotherThing(c); } else { printf("DoYetAnotherThing.\n"); // Optional error msg } // Because we know DoSomethingElse succeeded we have to free the // resources regardless of what happens in the nested code. CleanupSomethingElse(b); } else { printf("DoSomethingElse failed.\n"); // Optional error msg } // Because we know DoSomething succeeded we have to free the // resources regardless of what happens in the nested code. CleanupSomething(a); } else { printf("DoSomething failed.\n"); // Optional error msg } return result; }[/code] This function performs what it's supposed to beautifully. It's like a pyramid - you climb it on one side and descent on the other. Should you trip anywhere while climbing it, you get instantly teleported to the other end and start descending without reaching the top. Unfortunately all those nested if's make the pyramid rather large, and thus, code much less readable. So I was looking for an alternative coding style to do this. Here's something I came up with: [code]bool SomeFunction() { bool result = false; int a = DoSomething(); if (a == 0) goto CleanupA; int b = DoSomethingElse(); if (b == 0) goto CleanupB; int c = DoYetAnotherThing(); if (c == 0) goto CleanupC; ... etc ... // Final code here result = true; ... etc ... CleanupC: CleanupYetAnotherThing(c); CleanupB: CleanupSomethingElse(b); CleanupA: CleanupSomething(a); return result; }[/code] Okay, getting rid of those nested if's sure makes the code a lot more readable. But as any decent book on C++ will tell you do not use goto's in your code. I think I'll heed that advice. How about this: [code]int a, b, c; bool SomeFunction() { a = DoSomething(); if (a == 0) return false; b = DoSomethingElse(); if (b == 0) return false; c = DoYetAnotherThing(); if (c == 0) return false; ... etc ... // Final code here return true; } void SomeFunctionCleanup() { ... etc ... if (c != 0) CleanupYetAnotherThing(c); if (b != 0) CleanupSomethingElse(b); if (a != 0) CleanupSomething(a); }[/code] The problem here is obvious. Not only we have to split the routine in two functions, they both have to use global variables in order to share resources. I am not sure whether encapsulating this in a class would solve anything. What style should I use?
12. ## Feature Suggestion: Disable Default Constructor

I had a similar idea a while ago except that I would use an engine property to completely disable the use of default constructors (or destructors) in a script.
13. ## Package content naming convention

Hmm, interesting. I agree the standard URL syntax would probably be best to use for content inside an archive. I was going to use the 7zip library to store the content. I'm not sure about using virtual file systems - how should one implement it for a 7zip archive, and what happens when a user decides to forge an archive and feeds that to the program? Also, how is a program supposed to know which file to load, should two or more packages contain a file with the same name? Which one takes precedence? It should *IMO* simply be easier to just include a package name in the path. And since all package files are going to have the same file extension I thought about simply omitting it. The namespace example still looks the best bet although I admit I've got some more reading to do. SiCrane: Can you recommend any literature on this topic i.e. packages for game content?
14. ## Package content naming convention

Sorry, this confuses me. I was trying to do something similar to how unreal engine does it. It has packages with a ".u" extension that is not included in the path. The resource is simply referred to as package.content i.e. "Engine.Pawn" though I was unsure how to handle folders, and I would like a way to load multiple file types, not just script classes.
15. ## Package content naming convention

Eek, I'm not sure how to call this, but I'll ask in the newbie section as I currently have little experience with it. Okay, suppose I have a number of packed files (packages). Let's say they're all standard ZIP files. Actually the package format doesn't really matter, what's important is that the package can store different content in form of files like wav, bmp, jpg, avi, png, frag, vert, etc. The packages/archives can contain any file-type as well as other folders which may contain more files and/or subfolders. Basically a directory tree. What I am looking for is a decent (standard) way of referring to the content stored inside an archive so that a c++ program may extract a given resource file (based on the reference path) and load it into memory. I can't come up with a decent solution myself so I'd like to ask for some advice. As most good books on programming will teach you it's better to use an existing solution than to reinvent the wheel. For regular files stored on HDD one can probably use the Boost path. My approach works with files inside archives, so one has to be able to tell which archive we want to open and what file to load. i.e. Approach #1: package.resource We have an archive named "Test.zip" and we'd like to load a file "sound\master.wav". So if we refer to it as "test.sound.master.wav" how should the program know we're trying to load a file named "master.wav" that is stored inside package "test.zip" and located in subfolder "sound" as opposed to a file named "wav" (with no extension) stored inside Test.zip and located in subfolder "sound\master"? Approach #2: namespaces Same archive, same file. We can do "Test::sound\master.wav". This is much cleaner, but it's a bit awkward imo. Suggestions?