PDA

View Full Version : The General Programming Stuff Thread


Plutonic
1 May 2006, 02:16
Well here we go, a thread for people to talk about code and games in the making and what not without all those outsiders coming along and spamming it.. (Especially Plasma!!!)

I would start it off but it's late and I'm tired.

UnKnown X
1 May 2006, 11:08
Here's a tiny C++ programme that prints your computer's memory status. I was just testing out some of the stuff in the Windows header. (I compiled it in Dev-C++. I think you can remove the #define if you're using MSVC++.)#define _WIN32_WINNT 0x0500
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
const int MB = 1048576; // A MB is 1,048,576 bytes
MEMORYSTATUSEX statex;

statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);

cout << statex.dwMemoryLoad << " percent of memory is in use." << endl;
cout << endl;
cout << "There are " << statex.ullTotalPhys/MB << " total megabytes of physical memory." << endl;
cout << "There are " << statex.ullAvailPhys/MB;
printf(" (%.1f%%) free megabytes of physical memory.\n", (100*((float)statex.ullAvailPhys/(float)statex.ullTotalPhys)));
cout << endl;
cout << "There are " << statex.ullTotalPageFile/MB << " total megabytes of paging file." << endl;
cout << "There are " << statex.ullAvailPageFile/MB;
printf(" (%.1f%%) free megabytes of paging file.\n", (100*((float)statex.ullAvailPageFile/(float)statex.ullTotalPageFile)));
cout << endl;
cout << "There are " << statex.ullTotalVirtual/MB << " total megabytes of virtual memory." << endl;
cout << "There are " << statex.ullAvailVirtual/MB;
printf(" (%.1f%%) free megabytes of virtual memory.\n", (100*((float)statex.ullAvailVirtual/(float)statex.ullTotalVirtual)));
cin.sync();
cin.get();
return 0;
}

bonz
1 May 2006, 11:45
Well, at least the CODE tag here on the forum can now be fully used. :)

UnKnown X
1 May 2006, 11:48
Too bad that it removes a lot of whitespace, though.

Quoting the post should fix it, but it'd be nice if the code tags didn't get spaces removed.

M3ntal
1 May 2006, 15:18
Heh, first job: get Sel to make the CODE tag include whitespace, and syntax highlighting.

Anyone tried out C# yet? I'm currently learning it and i like it a lot.

UnKnown X
1 May 2006, 16:11
Actually, I prefer Python. Simple, quick and powerful (albeit a little slower than C and C++ and other compiled languages).

AndrewTaylor
1 May 2006, 16:30
Anyone tried out C# yet? I'm currently learning it and i like it a lot.
I used it a bit: I made the Crazy Dance Screensaver in it, for example. I've not really got my head around it yet, mostly because that was a one-afternoon project and I haven't done anything much else with it.

Plutonic
1 May 2006, 16:55
Can't say I have used C#, mostly because I have't looked into what its all about yet, is it worth looking at or should I just stick to C/C++?

M3ntal
1 May 2006, 17:08
It's like a cross between C++ and Java. It's a language that was invented for the .NET Framework, and given that all Visual Studio .NET languages are compiled into the same intermediate byte-code language and then run in a virtual machine (even C++), it would seem to be the current best language for Windows-based programming.

It has the simplicity of the Java language, but it also has a lot of the more advanced features of C/C++ like function pointers (called delegates in C#).

MadEwokHerd
1 May 2006, 17:41
Oddly enough, I hate both C++ and Java but kind of like C#. I'm more comfortable working in python though..

Pickleworm
1 May 2006, 20:00
Heh, first job: get Sel to make the CODE tag include whitespace, and syntax highlighting.


I could've sworn that the quote tag used to have syntax highlighting...

I also use Python a lot (I made a CGI QDB-style thing in it).

Can we also use this thread for general questions? I'm wondering what the HTTP headers (like, the Content-type: section) are for a Flash movie. Does anyone happen to know?

SupSuper
1 May 2006, 20:08
I think the content-type is application/x-shockwave-flash

Pickleworm
1 May 2006, 20:35
I think the content-type is application/x-shockwave-flash

Yep. Thanks a million.

Plutonic
6 May 2006, 18:59
Right, I've finally finished off the report for my 3rd year uni project. Which means, in between revising sleeping and possibly drinking I can start turning said 'project' into a fully functional game. I will get the website for progress up and running shortly, in the meentime here is a screeny or two :)

Clearly is a Micromachines clone, will be working on the weaponry system once I've finished off debugging what I have so far.

bonz
6 May 2006, 19:23
Clearly is a Micromachines clone, will be working on the weaponry system once I've finished off debugging what I have so far.
Wow!
Looks very good.

SupSuper
6 May 2006, 19:32
Indeed, a 3D MM clone is something to look forward to.
I'd suggest moving the meters to somewhere less intrusive on the screen, like corners, or even make them just plain numbered counters. Iirc, speed is pretty consistent in MM.

Paul.Power
6 May 2006, 20:37
I write computer programs by thinking "How would I do this in QBASIC?" and translating.

In a tight spot (read, "I can't be bothered to write a program that'll let me graph my data"), I get Excel to bail me out.

Oh yes, I am a bad programmer.

UnKnown X
6 May 2006, 21:39
Can't be as bad as me. I simply refuse to learn about pointers and classes and whatnot in any language, simply because it requires just a tad bit of effort.
So you can only imagine what I feel about programming graphics and stuff.

Paul.Power
6 May 2006, 22:37
Can't be as bad as me. I simply refuse to learn about pointers and classes and whatnot in any language, simply because it requires just a tad bit of effort.
So you can only imagine what I feel about programming graphics and stuff.Oh, yes, I do that too. That's all part and parcel of the QBASIC philosophy.

SupSuper
7 May 2006, 00:55
Can't be as bad as me. I simply refuse to learn about pointers and classes and whatnot in any language, simply because it requires just a tad bit of effort.
So you can only imagine what I feel about programming graphics and stuff.Lol, me too. That's one thing I love about making Flash games, I don't have to worry about classes or pointers or programming graphics. :p

AndrewTaylor
7 May 2006, 01:01
I've never bothered much with classes. There isn't actually anything you can do with classes that you can't do without them. Pointers are useful, but things like QBASIC manage them for you. You get a bit more flexibility using them explicitly, but I think it's just really speed increases in the end.

M3ntal
7 May 2006, 02:18
Pointer: variable that stores the address where a piece of data is stored, rather than the data itself.

C++:

/* declare normal int variable and initialise to hold value 10. */
int a = 10;

/* declare pointer to an int, assign it to some new space in memory to store an
int, and initialise that memory to hold value 20 */
int * b = new int(20);

a = 11; // assign normal int to 11
*b = 21; // assign 21 to the address the pointer points to.

/*
b actually stores an address because it is a pointer, so "b = 20" would tell it to
point to memory address 20, when we in fact want to store the value 20 in
the address it currently points to. Using the asterisk is called dereferencing, and any
actions performed to a dereferenced pointer happen to the address it points at
rather than the pointer itself. We just dereferenced b to get it to store
21 in the address it points to.
*/

a++;
b++;
/* a has just been incremented by 1, meaning it now holds the value 12.
b has also been incremented by 1, meaning it now points to the address in
memory that is 1 integer size after the address with our stored integer in.
incrementing b has NOT changed the value in the address it was pointing to 22,
there is currently nothing pointing to our value, which is still 21, and is considered
a "memory leak" (although, we could just decrement b by 1 again and reclaim it).
Also note that if we now dereference b, we don't know what will happen as the
address it now points to could contain anything. Lets get back to a "safe" state
again while we can, assuming nothing has already overwriten our stored value of 21: */

b--;

/* This is how to increment the value stored rather than the address; by dereferencing: */

*b++;

/* Now the address that b points to contains the value 22. */


Class: a structure that can store numerous pieces of data, and functions to perform operations on that data.

C++:

/* We need to use the namespace "std" so we can use strings. */
using namespace std;

class ForumMember {

/* "private" means that only functions within this class can access the data, "public"
means it is accessible from anywhere within the program */

private:
string username;
int sarcasm;

public:
/* the following is a "constructor". This function is run whenever a new object of
this class is created. It always has the same name as the class itself, never has a
return value, and can take any number of parameters. They are used for
initialisation. */
ForumMember(string _username, string _sarcasm) {
username = _username;
sarcasm = _sarcasm;
}

/* declare what functions are in this class. We'll have some functions to get and
set the data stored in this class, as it is private and cannot be retrieved directly. */

string getUsername() {
return username;
}

int getSarcasm() {
return sarcasm;
}

void setUsername(string _username) {
username =_username;
}

void setSarcasm(int _sarcasm) {
sarcasm = _sarcasm;
}

} // End of class

/* Now for our main method: */

void main() {

/* Create a new object from our class. Notice how we pass in the username and sarcasm
to the constructor, which we defined earlier. /*

ForumMember andrewtaylor = new ForumMember("AndrewTaylor", "32767");

/* We now have an object called "andrewtaylor" that has had its username variable set
to "AndrewTaylor" and its sarcasm variable set to 32767 (the maximum value a signed
16-bit integer can hold, heh). */

andrewtaylor.sarcasm = 100;
/* The previous line of code would cause an error as the sarcasm variable has been declared
as private, therefore it can only be accessed by code inside the class. We are currently
in the main program function which is not inside the class. The proper way to alter the
value of AndrewTaylor's sarcasm is to use one of those "set" function we made earlier
in the class, as they are declared "public" so we can access them from here: */

andrewtaylor.setSarcasm(100);

/* AndrewTaylors sarcasm has now successfully been reduced by lots. Oh, if only we could
do this in real life. Now, we have finished with our andrewtaylor object, so to free up
the memory it is using, we call the "destructor" using the "delete" keyword. This is the
opposite of a constructor (called with the "new" keyword), and can also be defined in
the class by us identically to the way we defined the constructor if we wish. the only
difference is the destructor has a tilde (~) before its name, so would be called
"~ForumMember()" instead. */

delete andrewtaylor;

/* AndrewTaylor is now dead :) */

}


Sorry, got carried away a bit.

::Edit::
It's easier to follow with indentation, so might be worth hitting the quote button and copy/pasting the lot to a text file.

SomePerson
7 May 2006, 03:48
What I haven't been able to figure out is how drawing to the screen works. I mean, to be able to go full screen and control what the screen shows, like happens in video games. Does that use OpenGL or something, or can it just be done using plain C++?

Edit: I mean, it seems most logical to me that I could create a matrix that holds the color values for each pixel and simply call some function to put it on the screen, but I have a feeling it's not nearly that simple.

MadEwokHerd
7 May 2006, 05:24
What I haven't been able to figure out is how drawing to the screen works. I mean, to be able to go full screen and control what the screen shows, like happens in video games. Does that use OpenGL or something, or can it just be done using plain C++?

The schemes I know of:

GDI (windows): You make a normal window that happens to cover the whole screen.

DirectDraw (windows): You use drivers that let you cheat and get past GDI because GDI is too slow (except I'm not sure if it is anymore).

Direct3D (windows): Same as DirectDraw, but with 3d hardware acceleration.

OpenGL (windows/mac/linux): Like Direct3D, but more open and widely available.

X Windows (linux): I dunno, compare it to GDI for windows. Modern window managers for X have a specific protocol for telling them your window is fullscreen so they can manage it properly.

X Windows with GLX (linux): Wait, that's OpenGL. Never mind.

Talk to hardware (I dunno, DOS?): You write to the hardware directly. Or maybe you use interrupts. Something that makes me sound smart. No one does this anymore, do they?

UnKnown X
7 May 2006, 07:22
I kind of know the pointer and classes basics, but I've never really been bothered learning how to use them properly. :p

AndrewTaylor
7 May 2006, 10:06
What I haven't been able to figure out is how drawing to the screen works. I mean, to be able to go full screen and control what the screen shows, like happens in video games. Does that use OpenGL or something, or can it just be done using plain C++?
I tend to get a library to draw to the screen. If I'm in 2D I'll use one of many, many available ones, and I'll usually just use the set screen resolution and blit to screen commands. I like to handles sprites and text and things myself. If I'm in 3D I've always used OpenGL, which is, I think, suprisingly simple to use if you can get your head around it.

I just have no interest in coding hardware to draw things to the screen. That's no fun to me -- it really just means learning anew every time a new system comes out, and it's difficult and you'll only need to do it once because you can then re-use the code for everything. So I reuse someone else's. But coding a game, with level generation and movement controls and AI and clever mechanics; that is an interesting challenge which is rarely the same twice.

M3ntal
7 May 2006, 15:46
You can't do it in Windows using plain C++ because Windows won't let you access the graphics hardware directly, you have to use a graphics API like GDI, DirectX, or OpenGL that is compatible with your graphics card drivers, and access it through these drivers.

Plutonic
7 May 2006, 16:08
SDL is pretty good for 2D stuff.

SupSuper
7 May 2006, 17:23
Shame K^2 isn't here anymore to tell us how it's much better to do everything hardcoded than using engines and whatnot. :p

SomePerson
7 May 2006, 19:26
Thanks! I guess I have some books to read now...

Shame K^2 isn't here anymore to tell us how it's much better to do everything hardcoded than using engines and whatnot. :p
I was just thinking that this morning. This place just isn't the same without him.:(

M3ntal
7 May 2006, 23:42
SDL is pretty good for 2D stuff.
Yeah, SDL looks pretty sweet actually. It's cross-platform, and uses whatever API is available. For example, it uses DirectX on Windows.

AndrewTaylor
9 May 2006, 12:14
I've got the Visual Studio Express. That just seems to use .NET for everything, which it has to be said works pretty well. It's annoying having to require .NET though, as most people don't have it.

MadEwokHerd
10 May 2006, 03:27
Especially people who don't use windows.

Although I suppose I could try mono..

AndrewTaylor
10 May 2006, 12:30
Yes, but if you don't use Windows you can't run any Windows EXE. Emulation is all well and good, but if you've written good code then it ought to be possible to port it properly. Really, only code that deals with window UI controls ought be unportable. If your input and graphics and so forth are properly wrapped you should be able to almost swap-out the Windows libraries for a MacOS or Linux one and recompile.

Really, someone should come up with a proper open virtual machine.

M3ntal
10 May 2006, 17:48
.NET *has* a virtual machine Andrew, and is an open standard. The files created from compiling a .NET application in visual studio are cross-platform byte-code (MSIL), they are then further compiled into native code upon first execution (meaning the first time you run the program it runs slow, but then runs full speed subsequently). You can run these programs on anything that has a .NET Framework implementation.

Mono is a .NET Framework implementation for Linux, i don't believe anyone has made one for MacOS yet.

MadEwokHerd
10 May 2006, 17:51
Mono provides the necessary software to develop and run .NET client and server applications on Linux, Solaris, Mac OS X, Windows, and Unix.

So yes, someone has. Unless you mean the older MacOSs.

SupSuper
10 May 2006, 20:26
For anyone that knows ActionScript:

I'm using loadVariablesNum() to get variables from a PHP script into Flash. Problem is, Flash only "gets" the variables after I press some button. Even with the code on the first frame, the variables are undefined until I press a button. Any button. How do I fix this?

M3ntal
10 May 2006, 22:17
I don't know actionscript, but is there a code section for the movie's loading event? ie, before it even gets to the first frame?

::Edit::
Something like this?

myMovie.onLoad = function(){
loadVariablesNum("script.php", 0, "GET");
}

SupSuper
11 May 2006, 17:26
It does (sorta) but it doesn't help, though I found the problem. Flash tries to display the variables before they're fully loaded, so I put in a delay in the code to wait for them to load and then display, and it works! :)

AndrewTaylor
11 May 2006, 19:04
.NET *has* a virtual machine Andrew, and is an open standard. The files created from compiling a .NET application in visual studio are cross-platform byte-code (MSIL), they are then further compiled into native code upon first execution (meaning the first time you run the program it runs slow, but then runs full speed subsequently). You can run these programs on anything that has a .NET Framework implementation.

Mono is a .NET Framework implementation for Linux, i don't believe anyone has made one for MacOS yet.
I knew it had a virtual machine, but as I didn't know about Mono that just seemed inconvenient. A one-platform-only virtual machine seemed a tad pointless. And I'm very suprised if it's fully open. That's not how Microsoft usually operate.

M3ntal
11 May 2006, 23:38
Yeah, they stole Borland's lead programming team (the guys who made Delphi) and told them to rip-off J2EE.

AndrewTaylor
12 May 2006, 22:37
That sounds more like Microsoft's modus operandi, yes.

Plutonic
23 May 2006, 23:08
Ok, I've just had an idea for a program for the worming community (to be announced once its more than a few buttons that dont do anything) and having never actually bother to try and program the windows API, I've not overly unsupprisingly ran into a few problems....

Has anyone here ever used the bloody thing?

Anyway, heres the problem:

I'm using VC++ 6, and using resource files to make a dialog-box application.
I've managed to get input boxes, dropdowns etc working and collecting the input correctly. What I now need is too place an image on the dialog box and be able to change it. I dont care what format the file to be changed to is,at this point, it could be on disk or an array of bytes.

So far I can place a bitmap on the dialog using the resource editor tools but cant seem to change it at runtime. Equally, I could load in a bitmap using fopen and stuff, but then dont know where to send it to get it to display!

Any help welcome!

SupSuper
23 May 2006, 23:19
I've never used VC++, just Borland stuff, but shouldn't there be an Image component that you can place on your dialog and then load whatever image you want into it at runtime?

Plutonic
23 May 2006, 23:52
You can definatly load an image on to the diag, but changing it at runtime is proving difficult...

Edit 1: ok, i have solved the image issue, found a tutorial on it after a few ours of googling. now.. anyone know how to use slidebars?!

Edit 2: Nevermind, fixed it :)
Now onto the meat!!

Pigbuster
16 Jan 2008, 03:21
Why doesn't full screen work in actionscript?

WHY.

Every time I try to do it, it acts like I just opened the .swf and chose "Fullscreen" from the menu. It gets big, but there's no hardware acceleration, so it's all slow.
I want it to do it like YouTube does it.

And I want to get it working in AIR (http://labs.adobe.com/technologies/air/), too. It doesn't, even when I copy and paste code directly from the documentation that says "THIS WILL WORK". :(