Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 07 Oct 2010
Offline Last Active Jan 12 2013 11:13 PM

#4998354 The Singleton Pattern: To be or not to be [used]?

Posted by on 07 November 2012 - 06:25 AM

I'm aware of the numerous problems that can arise through the use of the Singleton pattern, which some go as far as calling an anti-pattern... One of the biggest problems is all the references to the global instance/state, for example:

[source lang="csharp"]// inside method body...var obj = Something.Get();obj.TakeAction();obj.Whatever();[/source]

This can really complicate testing and make code that's difficult to maintain...

However, I'm wondering if the Singleton pattern is not ok (or even optimal) for certain circumstances when only one, globally accessible instance of something should exist...

Consider the class I'm currently working on. It is called "NativeMemoryManager", and it is essentially a "safe" API for allocating, manipulating and releasing unmanaged process memory within a .NET application (it uses a 1:1 wrapper I wrote of the CRT memory management API as its back-end -- this is all part of my generalized application & game development framework). NativeMemoryManager inherits "DisposableObject", which is an abstract class that offers a robust implementation of the IDisposable interface; all instances of which are pushed into the "ObjectTable" which is traversed at application shutdown to destroy potential memory leaks...

Only one instance of NativeMemoryManager should ever exist. It's only field is a data table which stores critical information about all memory allocations to prevent memory errors and free any unfreed memory when the application is shutdown. It doesn't need multiple states, and I don't foresee myself ever wanting to replace the singleton instance with some sort of testing instance. Also, if I implement any objects which need to use/consume NativeMemoryManager I will make that object accept the instance in its constructor and store it locally:

[source lang="csharp"]public SomeType( NativeMemoryManager mem ){ this.nativeMem = mem;}[/source]

Then it will use the reference its stores rather than calling NativeMemoryManager.Obtain() over and over...

So considering this particular case and others like it, do you see any problem with using the Singleton pattern? Or is there a better way to approach this?

#4997883 How powerful is Java?

Posted by on 06 November 2012 - 12:06 AM

@ Aeramor:

I agree with pretty much everything you said. I've never liked Java. I won't say language A sucks or language B is the greatest, but Java always seemed slow and unstable to me, whereas C# applications are snappy and responsive like C/C++ applications. I also agree that Java IDEs don't even hold a candle to Visual Studio; its really the best IDE for Windows available, and you can also of course use it to develop cross-platform applications.

However, I disagree with this:

If your end goal is to be an engine developer somewhere like Epic or Rockstar games you need to be learning C++ [...]

Yes, he does need to be learning C++... but not because it's the only path to being an engine developer, but because knowing C/C++ is an essential skill for any game programmer worth his salt! The 2000-teens are going to be the era of the managed engine. For years gamers have been accepting a lot of unstable code (consider Skyrim's tendency to CTD and lose textures at run-time) because applications/games are growing so large and complex that no mere mortal can manage all of its memory and resources. In this context C# and the CLR looks more appealing every day because its gains in memory efficiency, stability and type-safety are beginning to make it a more performant choice than native languages. This is precisely why my company is developing a managed engine in C# instead of using the traditional approach of C++...



#4997610 How powerful is Java?

Posted by on 05 November 2012 - 10:04 AM

Programming languages do not have any quality or property called "power"... So you'll never see anything like:

Language: C++
Horsepower: 700
Torque: 580lb-ft

There's simply no such thing... Posted Image

Some languages perform differently from others, but there are no hard-set rules about performance either. Generally, languages compiled to machine code (native binary for the CPU) are faster than "JIT'ed" managed languages which are faster than interpreted languages. But there are even exceptions to this general rule... On quite a few occasions I've written and tested C/C++ and C# code side-by-side and seen the C# code out-perform the native code. There are various articles you can find online if you're more interested in language performance.

Any language that is Turing-complete (which is pretty much every programming language) is theoretically capable of calculating/simulating the entire history of the known universe; provided it runs on a system with unlimited memory. Therefore there's no reason you cannot create the same game in C, C++, Java, C#, Visual BASIC or assembly language.



#4995910 Create an operating system

Posted by on 31 October 2012 - 12:38 PM

Here is an absolutely wonderful operating system development tutorial series by "Brokenthorn"... this helped me immensely in learning how to develop operating systems in Visual Studio, making working kernels and drivers as PE images (EXE and DLL files), configuring Visual Studio to compile properly, assemble assembly code, automatically deploy your kernel on your virtual machine for debugging and much more. It also has tutorials on implementing both basic and advanced graphics shells with working code and implementing a basic C Standard Lib and C++ runtime. It's awesome... trust me... :-)


#4995511 High-level and Low-level languages.

Posted by on 30 October 2012 - 12:37 PM

What languages are "low-level" and "high-level" can be a matter of perspective... A "low-level" language is a language which is more closely attuned to the underlying architecture of the machine, whereas a "high-level" language is more "human-friendly" and abstracted...

If you ask a web developer or Python programmer what C is they will tell you C is a "low-level" language... but if you ask an OS programmer from a Linux project they will probably tell you C is a "portable, high-level programming language"... However, all will tend to agree that assembly languages are low-level because there is generally no abstraction whatsoever -- just op-codes that represent binary operands...

#4995302 A class that contains an instance of itself

Posted by on 29 October 2012 - 11:51 PM

I of course tried this, but I asked this because I didn't know what to put in the constructor
[source lang="cpp"]Menu::Menu(){subMenu=new Menu()//??}[/source]

If you define an object type which invokes its own constructor within its constructor (like you've done above) it creates an infinite recursion, which is similar to an endless loop. Example ::

[source lang="cpp"]class MyClass{public: MyClass() { myClass = new MyClass(); } MyClass *myClass;};void main() { MyClass* mc = new MyClass;}[/source]

As soon as the "main" function of this program starts it creates a new MyClass instance and invokes its constructor with the new operator... and then what happens? Control jumps to the MyClass constructor, MyClass::MyClass()... but the code in the constructor AGAIN invokes the constructor with the new operator, in an attempt to instantiate the myClass instance... but that myClass instance will have its own myClass instance too lol... So the constructor is invoked again, and again and again and never stops... at least not until you terminate the program! ;-)

A better way to handle this dilemma is allow the constructor in your program to take a Menu* parameter for its sub-menu... let outside code create the sub-menu (if the menu has one) and pass it to the new, primary menu instance. Alternatively you could offer a static (or even an instance) method in the Menu class, like [static] Menu::AddSubMenu(Menu* primary, <creation params> ) or [instance] Menu::AddSubMenu(...) and let that method handle adding the sub-menu to the primary menu...

#4994860 Create an operating system

Posted by on 28 October 2012 - 04:54 PM

can u people quit discussing os development. All i want is something to create an inmteractive program. If coding is needed, I would prefer python or java and btw, whats svg

Well, your thread suggests you were interested in OS development... if you're not, then I have no idea what you're trying to do (or better yet, why or what the point is)... You're initial question has already been answered though, just use whatever graphics API you're best with... Anything will work...

#4994646 Create an operating system

Posted by on 28 October 2012 - 12:53 AM

@ Shrinkage ::

Very good and interesting post... I +1'ed it!

However, I'd like to point out a few things... For starters, while your idea is great and makes me want to try it myself, which of the two is cheaper: 1) free download of Sun's Virtualbox and/or free download of Microsoft's Virtual PC 2) going out and buying physical hardware like you describe? Obviously, #1 is a lot cheaper and more accessible for "the crowd". Secondly, most of us programmers are writing software for x86/64 systems. And when we want to learn about OS development it's usually not to try to become a professional OS developer (very tough market to get into, dominated by Microsoft) but to go on an "academic" venture; to become more knowledgeable and skillful programmers on our primary development platform. So for those two reasons alone I would not go as far as saying that the x86 platform is a "horrible" choice... I think that if you're in OS development for academic reasons you should work with the platform you do most of your userland/everyday programming on... and if you're in it to make a profession out of OS development you should work with the platform you intend to support.

However, I must agree that learning the x86/64 architecture is quite difficult; the learning curve is steep, the manuals are thick and Intel is not exactly what I'd call a "n00b-friendly" company lol. There are lots of little tricky caveats, traps and pitfalls... lots of weird things you must learn, like setting up a GDT (global descriptor table) as an off-the-wall example... But if you learn this stuff you walk away knowing what makes your PC and your primary OS (be it Windows, Linux, etc) tick. It's hard to even describe in words how incredibly valuable that can be to a PC programmer... say you're writing an performance-critical function that needs to move and/or compare large blocks of memory as quickly as possible. Without experience in low-level/systems programming, you're like to just to take the most direct, straight-forward (and naive) approach to your algorithm. With such experience, however, you know how the CPU, data bus and RAM work... you know how memory is moved in/out of registers, to and from RAM and understand virtual memory and paging/caching... you would also know that on 32-bit architecture the CPU registers are 32-bits in size and natively move d-words (32-bits of data) faster than it can move 64-bits (and vice-versa on x64 platforms). Armed with that sort of knowledge you will almost always know how to implement the best possible routines. However, if you're a PC programmer by trade and all your OS development experience is on some completely different platform there can be quite a few gaps in your knowledge...

So I encourage people to work with the platform that's right for them... and if you're a PC developer then you may as well bite the bullet and learn the x86/64 platform. If, however, you are wanting to make a career or serious hobby out of OS dev then choose the platform you need to work with and heed Shrinkage's advice... and learn more than one platform!



#4994600 Should rendering geometry and physics geometry separated?

Posted by on 27 October 2012 - 07:15 PM

Absolutely... And there should be, in my opinion, a complete separation from visual game entities and logical/physical game entities! For a physics object, for example, the visual representation should do nothing more than mirror the transformations of the logical representation. The logical representation holds the physics hull and all of the calculations are made against it, whereas the visual representation just follows it and gets rendered.

#4994548 Create an operating system

Posted by on 27 October 2012 - 04:07 PM

To demonstrate what Washu is talking about, a correct example of what I was trying to demonstrate earlier would be more like this::

[source lang="cpp"]void _writeText( const char* str ) { int index = 0x00; char* pstr = (char *)str; unsigned short *vidMem = (unsigned short *)0xB8000; for( ; *pstr; ++pstr, ++index ) { unsigned char c = *pstr; vidMem[index] = (unsigned short) c | 0x0700; }}[/source]

Sorry for screwing this up multiple times trying to make a simple point lol... Like I said, it's been years and I can't copy code from any of my projects verbatim because there's a lot of code, headers, macros and functions used in numerous files and it wouldn't make sense without posting it all (overkill for such a small, contrived example). So I just have to either try to strike up examples from memory or write a new, simplified version; as it so happened I did that wrong, lol... :P

Hopefully this time I did the demonstration correctly haha

#4994538 Create an operating system

Posted by on 27 October 2012 - 03:49 PM

Well, the bad news is, that code is STILL wrong.
The text screen video memory at that address takes WORDS, not bytes. Each word is a combination of a character to display and an attribute describing the color of the character and its background.

Ah, got me again... Oddly though, using char* did work, but in the real code I was padding it with the color information to write 16-bits for each char... But I looked at my text I/O implementation of a more mature version of my system project and saw that I actually used type unsigned short*, so you're correct. It's been a couple years though, like I said... so my memory of some things is a bit faded. Posted Image

Anyhow... about learning resources... It is indeed true that there are plenty books on OS development. But very few (in my experience) were suitable for beginners and even fewer actually teach how to write your first OS. Most books assume you're a CS major with system's programming experience and teach about OS design/architecture as opposed to the "how" and "why". So as a beginner OS programmer it's extremely difficult to find the learning resources you need. It's been a couple years since I did my OS project, so maybe things have changed; but that is the way I remember it.

Just reading ATC's response scared the everliving shit out of me, and I was not (and never would) consider trying to build a new OS.

Well, I'm off to have nightmares of implementing malloc and free...

Well, I didn't mean to scare ya lol! Posted Image

It's actually a lot of fun... and the feeling of overwhelming satisfaction you get from making the damn thing work for the first time was one of the most rewarding programming experiences I've ever had. Writing you own CSTDLib functions like malloc and free is also extremely rewarding. It's difficult and frustrating to get through it but when you get it working correctly you will be extremely happy and proud of yourself. Each time you call malloc or free you'll be grinning ear to ear knowing you made that code work. :-)

Even the little things like rolling your own strlen or strcmp functions are fun:

[source lang="cpp"]// Naive approach; just an example ::size_t strlen( const char* str ) { size_t len; char* ptr = (char *)str; for( len = 0x00; *ptr != '\0'; ++ptr, ++len ) ; return len;}[/source]

Seriously, you should try your hand at it! It's really challenging and entertaining to roll your own standard lib and try to write optimal routines and functions. And you'll walk away knowing 1000 fold more about C, C++, assembly language, the standard libraries and computer science.

One thing that was unique about my OS project is that I developed it completely within Visual Studio 2008 (Pro). Yes, you can indeed write an OS in Visual Studio! It's all about configuring the environment and project settings correctly, and either writing your own bootloader capable of handling your kernel/loader or implementing the multi-boot standard to make your OS interoperable with a good bootloader like GRUB.

I chose the latter approach, and used GRUB. And my kernel was actually a PE image (.exe file). The way I achieved interoperability with GRUB was by setting the base address of the PE image to the system loader's entry point, and using some assembly language tricks to emit the multi-boot header right there where GRUB would be expecting it. For example:

[source lang="cpp"]#define dd(x) \ __asm _emit (x) & 0xff \ __asm _emit (x) >> 8 & 0xff \ __asm _emit (x) >> 16 & 0xff \ __asm _emit (x) >> 24 & 0xff[/source]

That macro will actually embed a dword right into the code. So you can do this:

[source lang="cpp"]int _loaderMain( ){ dd( MB_MAGIC_NUMBER ); // ... and so on ...}[/source]

I also went so far as to write an entire static lib for handling PE images and reading their headers which could be linked right into the kernel or anything else that needed it. I implemented drivers as DLLs with a special *.DRV extension, and the OS loader (an intermediate loading program loaded by the bootloader that loaded the kernel and critical drivers) could us the "PELib" to read the kernel and driver headers. Just talking about it kinda makes me want to start playing with it all again, but I've got an engine to write lol. Posted Image



#4994344 Create an operating system

Posted by on 26 October 2012 - 10:58 PM

As someone who's actually written a working OS before, I would recommend you don't even waste your time making this GUI "prototype" unless:

A) You already have experience and know how to write an OS

B) You have not completed A, so you stop what you're doing to complete A... lol

Writing an operating system is one of the most difficult programming tasks you could ever hope to do. But I do think you should do it! Not only is it fun but the things you will learn from it will make you twice the programmer you were before. It's an "academic" project I think every serious programmer should try. You're going to need intimate knowledge of not only C and C++ but also assembly language for the processor(s) you are targeting/supporting. Apart from that you need an extremely thorough understanding of memory and how the hardware you're targeting works. After all, you're programming not in "userland" but in Ring-0, where you have direct access to physical RAM and hardware.

When I say "direct access to physical memory" I mean that literally. For example, on x86 architecture when an OS is running in PMode (protected, 32-bit memory mode) with video memory in color text mode, the contents of video memory are in the address range 0x000B8000 to 0x000BFFFF. Want to write to video memory directly?

[source lang="cpp"]unsigned short* screen = (unsigned short *) 0xB8000;const char* str = "Hello, OS world!\0";char* pstr = (char *)str;for( ; *pstr != '\0'; ++pstr, ++screen) *screen = (unsigned short) (*pstr | 0x0700);[/source]

That's right... In Ring-0 you can cast any arbitrary unsigned integer into a pointer type and just directly read/write RAM. While this is extremely powerful, it can also be very "dangerous". If you don't know what you're writing where you could be corrupting important data or destroying bits of your kernel or critical system drivers in memory lol. So to develop any somewhat sophisticated operating system you'll need to implement a full-blown memory manager and implement a virtual memory. Hopefully that gives you an idea of how hard this is lol...

You're going to need to know pretty much everything about your CPU and hardware and how it all works to get anything done. For instance, do you know the significance of the address 0x007C00 on x86 architecture (place where BIOS loads bootloader)? There are tons of little ins and outs like that you must learn, and it takes a LOT of freakin time. Expect to be working on just getting a good bootloader and basic kernel started for several months if you're a beginner to OS programming. Your non-OS-related programming experience will be very helpful but cannot prepare you for what you're about to face. For example, you'll have to start coding your OS in C or assembly language. You cannot use C++ because you have no C++ runtime environment and no implementation of important language constructs like the new and delete operators. But oh wait, when you write your C code you can't use any of the C Standard Library because your OS doesn't have a CSTDLib yet! You've gotta write your own, complete with your own malloc and free implementations! :-)

So hopefully that makes it clear why I suggest you start learning OS programming instead of fooling around with a useless FLASH prototype that doesn't help you on your quest one bit. Worry about that prototype when you've got a good OS framework written and know what you're doing. Implementing good graphics and a GUI shell in your OS is an extremely advanced topic, and you will have had to write your own input drivers for mouse, keyboard, etc for it to be of any use... yes, that's another thing, you've gotta write your own drivers... you have to write everything yourself because a new OS project is a completely blank slate! XD

The quest you're about to embark upon is incredibly fun, as scary as my little speech might make it sound. And along the way it also gets very frustrating because there are virtually no resources available online or in book stores to help you. But I am here if you need some help. I'm not (by any means) a professional OS developer, but I know quite a bit about it from my past experience. I can provide you with learning resources and help coding (I've got some of my source saved on a USB flash drive). So feel free to pm me if you need any assistance. It's a topic I enjoy discussing because it's so "low-level" and intriguing!



#4994284 Best language to code my game

Posted by on 26 October 2012 - 05:03 PM

As always, ATC is here to recommend C# lol... Implementing the type of project you're talking about in C# would be a breeze. C#, imho, is a much more well-designed and fluid language than Java, and it's more efficient/fast at turning the thoughts and plans in your head into working software than C or C++. However, C++ is also a viable option.

#4992311 Problem consuming DLL in VS 2012 (native C++)

Posted by on 20 October 2012 - 06:22 PM

Geez, sometimes I can be a major idiot lol...

It was all because I forgot to use C-linkage and add my custom _DLLEXPORT modifier to things. So the problem is now solved...

If anyone else ever has this problem, here's what you need to do:

1) Go to Configuration Manager and set the proper project dependencies (in my case "ConsoleTest" depends on "ATCFramework")
2) Under your project properties of your consuming application, go to "Common Properties" and click "Add Reference"; then reference your DLL
3) Specify C-type linkage and export your classes, structures and functions like so:

[source lang="cpp"]extern "C"{ typedef struct __declspec(dllexport) MY_STRUCT { int X, Y; // whatever } MyStruct, *MyStructPTR;};[/source]

You only need to do this in your header files... not in your .c or .cpp implementation files. It's as simple as that! :-)

Have fun,



Note: Forcing C-linkage is not absolutely necessary for C++, but you are required to do so to consume something from C code. Otherwise you will get some name mangling and C code won't know what to do. So it's just good practice imo to force C-linkage in these situations. I also think it helps if you ever want to debug at the assembly/machine code level.

As an added note I don't think a function with C-linkage can return a C++ class instance. So watch what you're doing or you'll get weird linker errors that may or may not make any sense.

#4991575 Good C++11 resources for advanced programmer

Posted by on 18 October 2012 - 04:07 PM

Hey guys,

I'm wanting some learning resources for C++11 oriented toward advanced programmers. I already know C and C++ quite fluently. But since I haven't used either in a couple years for any serious/commercial work, I'm a bit rusty in certain areas. Some of the things I need to brush up on:
  • "const correctness"
  • the various cast operators and when and when not to use each
  • all of the stuff available in the 11 standard libraries and how to use them (I'm prone to reinvent wheels)
  • proper C++ software design practices and conventions
  • general best practices and pitfalls
  • proper template class design
In the past when I normally used C and C++ it was for actual systems programming, where often no implementation of the full standard libs and runtimes even existed (often had to roll my own malloc and free implementations). So while I know the syntax through-and-through, have intimate knowledge of pointers and memory and know how the language works/runs, I'm prone to "reinvent the wheel" in many situations because I didn't know the stdlibs already implemented the thing I needed. I also need to brush up on "const correctness", referencing, etc. And anything to help me better design classes, polymorphic interfaces, templates, operators, and software in general would also be very helpful. And I don't want to waste my time with beginners' books talking about variables being storage space for numbers. Posted Image

I'm not really doing this for any important project or commercial work... we're using C# for our projects. But I invested a lot of time (and money) in the past into my programming skills, which included C and C++. It's not an art I want to lose. And since I now have Visual Studio 2012 and C++11 is out and taking the programming world by storm I don't want to end up behind the times; I've got to keep up with the rest of the world.

I'm looking for any online references/materials, articles, tutorials, books, videos and anything and everything that can be helpful to me in re-mastering C++ and learning about the new C++11 standard.