|
Post by matthew on Oct 26, 2008 1:50:56 GMT -5
Now that a lot more people are starting to develop Plugins for Basic4GL I thought I would post some links to Sites which have helped me learn the basics of C++. - C++ quick reference cheatsheet can be downloaded from here.
- One of the best C++ language tutorials can be found here.
- The homepage of Bjarne Stroustrup, the man who developed C++.
- A lot of useful information can be found at the C++ FAQ Lite.
- The msdn reference library can be found here.
When I was writing my simple memPlugin I found this msdn page very useful. The Code::Blocks IDE with the GCC compiler & GDB debugger from MinGW can be downloaded from this site.
|
|
|
Post by Nicky Peter Hollyoake on Oct 26, 2008 9:37:35 GMT -5
Nice Matthew! definy come in handy for beginners! (including me on some parts) The second one is where I was learning from before. - Nicky
|
|
|
Post by Darkjester on Oct 26, 2008 13:09:19 GMT -5
For general use here is a quick list of the header files for the c++ specification. the headers marked with * are part of the STL librarys
algorithm.h* deque.h* functional.h* iosfwd.h iterator.h* locale.h new.h queue.h* stack.h string.h valarray.h
bitset.h exception.h iomanip.h iostream.h limits.h map.h* numeric.h* set.h* stdexcept.h typeinfo.h vector.h*
complex.h fstream.h ios.h istream.h list.h* memory.h* ostream.h sstream.h streambuf.h utility.h* cmath.h
|
|
|
Post by matthew on Oct 26, 2008 13:33:52 GMT -5
For those of you who may not want to use the Code::Blocks IDE there are several alternatives out there. Dev-C++ can be downloaded from here. It's pretty old nowadays & you may run into some issues using it on Vista. wxDev-C++ is the new version of Dev-C++ & can be downloaded from here. Both Dev-C++ & wxDev-C++ use the GCC compiler & GDB debugger from MinGW. Finally, Visual C++ Express Edition 2008 can be downloaded from here.
|
|
|
Post by Nicky Peter Hollyoake on Oct 26, 2008 15:44:22 GMT -5
Heres a code of mine, shows how stuff works. I'm sure someone told me that I shouldn't use "define" to much. Why?
#include <iostream> #include <cmath>
#define input cin >> #define print cout <<
using namespace std;
int main () {
/// Declare our vars int Array[5]; int Number;
/// the input print"type a number:"; input Number; print"\n";
for (int i = 0; i <= 5; i++) { Array[i] = (Number * i);
print sqrt(Array[i]); print "\n"; } }
|
|
|
Post by matthew on Oct 26, 2008 15:56:09 GMT -5
|
|
|
Post by Supermonkey on Oct 26, 2008 16:17:53 GMT -5
Another reason, especially with the example above is readability. If you release any source code which somone else may one day have to maintain you should avoid using #define statements like that, it's just not nice for others.
|
|
|
Post by Nicky Peter Hollyoake on Oct 26, 2008 19:40:22 GMT -5
Ok thanks. Can someone check through this to tell me if I done anything wrong, or something should be changed? I'm not 100% sure about "getchar()" I think thats C, but I couldn't see any other way round it. #include <iostream> #include <windows.h> #include <string> #include <conio.h>
struct sPlayer { int X; int Y; } s;
int main() {
sPlayer Player; sPlayer Temp; std::string Map[9][15];
/// Create map for (int i = 0; i <= 14; i++) { Map[0][i] = "#"; Map[8][i] = "#"; }
for (int i = 1; i <= 7; i++) { Map[i][0] = "#";Map[i][1] = " ";Map[i][2] = " ";Map[i][3] = " ";Map[i][4] = " ";Map[i][5] = " ";Map[i][6] = " ";Map[i][7] = " "; Map[i][8] = " ";Map[i][9] = " ";Map[i][10] = " ";Map[i][11] = " ";Map[i][12] = " ";Map[i][13] = " "; Map[i][14] = "#"; }
/// Players starting position Player.X = 2; Player.Y = 2;
std::string key; int Screen_on = 0; while (true) {
/// Players movement if (Screen_on) { key = getchar();
if (key == "w" or key == "W") { Player.Y--; }
if (key == "s" or key == "S") { Player.Y++; }
if (key == "a" or key == "A") { Player.X--; }
if (key == "d" or key == "D") { Player.X++; } }
if (Player.X < 1) { Player.X = 1; }
if (Player.X > 13) { Player.X = 13; }
if (Player.Y < 1) { Player.Y = 1; }
if (Player.Y > 7) { Player.Y = 7; }
/// if players move clear screen and update screen if (Temp.X < Player.X or Temp.X > Player.X or Temp.Y < Player.Y or Temp.Y > Player.Y or key != "") {
system("color 1c"); system("CLS");
for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 14; j++){
if (i == Player.Y and j == Player.X) { std::cout << "@"; }
else { std::cout << Map[i][j]; }
} std::cout << "\n"; } }
/// Get players last position Temp = Player;
/// set screen to on Screen_on = true; } }
w - up s - down a - left d - right you have to press the key then enter to make the move, couldn't figure out how to get the keys. - Nicky
|
|
|
Post by matthew on Oct 26, 2008 22:43:25 GMT -5
Stop using conio.h, it's non-standard.
If you want to detect a keypress stop using getchar(), it's C & start using std::cin.get().
Also now that you're using C++ you might want to stop using structures & start using classes as they've got a lot of advantages over structures.
|
|
|
Post by Darkjester on Oct 26, 2008 23:07:53 GMT -5
Structures are ok for use inside of classes according to one of those links at the top of this page regarding c++ reference
|
|
|
Post by matthew on Oct 26, 2008 23:30:40 GMT -5
There's a very good page here on Wikipedia about the usage of Structures & Classes in C++.
|
|
|
Post by Nicky Peter Hollyoake on Oct 27, 2008 9:59:37 GMT -5
Thanks Matthew I knew that it wasn't part of C++ but I just couldn't think the C++ way of doing it. updated code and better collision#include <iostream> #include <windows.h> #include <string>
struct sPlayer { int X; int Y; } s;
int main() {
sPlayer Player; sPlayer Temp; std::string Map[9][15];
/// Create map for (int i = 0; i <= 14; i++) { Map[0][i] = "#"; Map[8][i] = "#"; }
for (int i = 1; i <= 7; i++) { Map[i][0] = "#";Map[i][1] = " ";Map[i][2] = " ";Map[i][3] = " ";Map[i][4] = " ";Map[i][5] = " ";Map[i][6] = " ";Map[i][7] = " "; Map[i][8] = " ";Map[i][9] = " ";Map[i][10] = " ";Map[i][11] = " ";Map[i][12] = " ";Map[i][13] = " "; Map[i][14] = "#"; }
Map[5][4] = "#"; Map[5][5] = "#"; Map[5][6] = "#";
/// Players starting position Player.X = 2; Player.Y = 2;
std::string key; int Screen_on = 0; while (true) {
/// Players movement if (Screen_on) { key = std::cin.get();
if (key == "w" or key == "W") { Player.Y--; }
if (key == "s" or key == "S") { Player.Y++; }
if (key == "a" or key == "A") { Player.X--; }
if (key == "d" or key == "D") { Player.X++; }
if (Map[Player.Y][Player.X] == "#") { Player = Temp; } }
/// if players move clear screen and update screen if (Temp.X < Player.X or Temp.X > Player.X or Temp.Y < Player.Y or Temp.Y > Player.Y or key != "") {
system("color 1c"); system("CLS");
for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 14; j++){
if (i == Player.Y and j == Player.X) { std::cout << "@"; }
else { std::cout << Map[i][j]; }
} std::cout << "\n"; } }
/// Get players last position Temp = Player;
/// set screen to on Screen_on = true; } }
- Nicky
|
|
|
Post by Nicky Peter Hollyoake on Nov 3, 2008 10:15:47 GMT -5
OK, I'm done, everything I done in this is possible to do with Basic4GL I'm just showing how it is done in C++ though. /// Include files /// #include <iostream> #include <conio.h> #include <string> #include <windows.h> /// End of include files ///
void gotoxy(int x, int y) { static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); static COORD WritePos; WritePos.X = x; //-1 for borland compatibility?? WritePos.Y = y; SetConsoleCursorPosition(hStdOut,WritePos);
}
/// key function int Key () { int TheKey = 0;
if (kbhit()) { TheKey = getch(); }
return TheKey; }
/// Main int main() {
/// other vars int ControlKey;
/// Player vars int PlayerX = 1, PlayerY = 1; int TempX, TempY;
/// Make map const int MapWidth = 46; const int MapHeight = 19;
std::string Map[] = {"###############################################", "# #", "# ### #", "# # # #", "# @ ### #", "# ###### # # #", "# # # # #", "# # # # #", "# # # # #", "# # # # #", "# # ########### # #", "# # #", "# # #", "# # #", "# # #", "# ############################## #", "# #", "# #", "# #", "###############################################"};
/// Search for player position for (int i = 0; i <= MapHeight; i++) { for (int j = 0; j <= MapWidth; j++) {
if (Map[j] == '@') { Map[j] = ' '; PlayerX = j; PlayerY = i; } } }
int FirstLoop = false;
/// Colors system("Color 1c");
/// Start loop while (true) {
ControlKey = Key();
/// Remember the last position the player was at TempX = PlayerX; TempY = PlayerY;
/* Key numbers Left Arrow: 75 Up Arrow: 72 Down Arrow: 80 Right Arrow: 77 */
/// Controls if (ControlKey == 75) PlayerX--; // Left if (ControlKey == 77) PlayerX++; // Right if (ControlKey == 80) PlayerY++; // Down if (ControlKey == 72) PlayerY--; // Up
/// Collision // Keep the player in a box if (PlayerX < 0) PlayerX = 0; if (PlayerX > MapWidth) PlayerX = MapWidth; if (PlayerY < 0) PlayerY = 0; if (PlayerY > MapHeight) PlayerY = MapHeight;
// Wall collision (stops the player going through walls) if (Map[PlayerY][PlayerX] == '#') { PlayerX = TempX; PlayerY = TempY; }
/// Draw map if (PlayerX != TempX or PlayerY != TempY or FirstLoop == false) {
/// System command gotoxy(0,0);
for (int i = 0; i <= MapHeight; i++) { for (int j = 0; j <= MapWidth; j++) {
if (PlayerX == j and PlayerY == i) { std::cout << "@"; } else { std::cout << Map[j]; }
} std::cout << "\n"; } }
FirstLoop = true; } } - Nicky
|
|
|
Post by Darkjester on Nov 3, 2008 17:27:07 GMT -5
Good start nicky I do have one oppinion maybe clean up the big if then block a bit?
|
|
|
Post by smc44 on Nov 3, 2008 18:27:21 GMT -5
when u run this code: /// Include files /// #include <iostream> #include <conio.h> #include <string> #include <windows.h> /// End of include files ///
void gotoxy(int x, int y) { static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); static COORD WritePos; WritePos.X = x; //-1 for borland compatibility?? WritePos.Y = y; SetConsoleCursorPosition(hStdOut,WritePos);
}
/// key function int Key () { int TheKey = 0;
if (kbhit()) { TheKey = getch(); }
return TheKey; }
/// Main int main() {
/// other vars int ControlKey;
/// Player vars int PlayerX = 1, PlayerY = 1; int TempX, TempY;
/// Make map const int MapWidth = 46; const int MapHeight = 19;
std::string Map[] = {"###############################################", "# #", "# ### #", "# # # #", "# @ ### #", "# ###### # # #", "# # # # #", "# # # # #", "# # # # #", "# # # # #", "# # ########### # #", "# # #", "# # #", "# # #", "# # #", "# ############################## #", "# #", "# #", "# #", "###############################################"};
/// Search for player position for (int i = 0; i <= MapHeight; i++) { for (int j = 0; j <= MapWidth; j++) {
if (Map[i][j] == '@') { Map[i][j] = ' '; PlayerX = j; PlayerY = i; } } }
int FirstLoop = false;
/// Colors system("Color 1c");
/// Start loop while (true) {
ControlKey = Key();
/// Remember the last position the player was at TempX = PlayerX; TempY = PlayerY;
/* Key numbers Left Arrow: 75 Up Arrow: 72 Down Arrow: 80 Right Arrow: 77 */
/// Controls if (ControlKey == 75) PlayerX--; // Left if (ControlKey == 77) PlayerX++; // Right if (ControlKey == 80) PlayerY++; // Down if (ControlKey == 72) PlayerY--; // Up
/// Collision // Keep the player in a box if (PlayerX < 0) PlayerX = 0; if (PlayerX > MapWidth) PlayerX = MapWidth; if (PlayerY < 0) PlayerY = 0; if (PlayerY > MapHeight) PlayerY = MapHeight;
// Wall collision (stops the player going through walls) if (Map[PlayerY][PlayerX] == '#') { PlayerX = TempX; PlayerY = TempY; }
/// Draw map if (PlayerX != TempX or PlayerY != TempY or FirstLoop == false) {
/// System command gotoxy(0,0);
for (int i = 0; i <= MapHeight; i++) { for (int j = 0; j <= MapWidth; j++) {
if (PlayerX == j and PlayerY == i) { std::cout << "@"; } else { std::cout << Map[i][j]; }
} std::cout << "\n"; } }
FirstLoop = true; } }
.......is it a suppose to do this lol it also makes really annoying beeeping noises that are really loud over and over agian here goes the picture
|
|