Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Doom Metal Poser

CHAR_INFO

This topic is 5573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Any ideas why this isn''t working?
#include <iostream>
#include <fstream>
#include <windows.h>
#include <stdlib.h>
using namespace std;

void draw_map(CHAR_INFO screenBuffer[]);
void clear_screen();
bool move_player(CHAR_INFO screenBuffer[]);

int grid_width, grid_height, indentx, indenty;

struct player{
    int posX;
    int posY;
};

player p1;

int main()
{
     srand(GetTickCount());
     char ch;
     ifstream fin; fin.open("test.txt");
     
     fin >> grid_width; fin >> grid_height;
     fin >> indentx; fin >> indenty;
     fin >> p1.posX; fin >> p1.posY;
     
     CHAR_INFO screenBuffer[grid_width * grid_height];
     
     for(int y=0; y < grid_height; y++) {
         for(int x=0; x < grid_width; x++) {
             fin >> ch;
             switch(ch) {
                 case ''0'':
                     screenBuffer[x + y * grid_width].Char.AsciiChar = '' '';
                     screenBuffer[x + y * grid_width].Attributes = 0;
                     break;
                 case ''1'':
                     screenBuffer[x + y * grid_width].Char.AsciiChar = ch;
                     screenBuffer[x + y * grid_width].Attributes = FOREGROUND_BLUE | FOREGROUND_GREEN;
                     break;
             }
         }
     }
     
     draw_map(screenBuffer);
     
     while(1) {
         if(move_player(screenBuffer)) {
             clear_screen();
             draw_map(screenBuffer);
         }
     }
     
     system("pause");
     exit(0);
}

void draw_map(CHAR_INFO screenBuffer[]) {
    SMALL_RECT draw_rect = {indentx, indenty, indentx + (grid_width - 1), indenty + (grid_height - 1)};
    COORD bufferSize = {grid_width, grid_height};
    COORD zeroZero = {0, 0};
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    
    screenBuffer[p1.posX + p1.posY * grid_width].Char.AsciiChar = 1;
    screenBuffer[p1.posX + p1.posY * grid_width].Attributes = FOREGROUND_RED | FOREGROUND_INTENSITY;
    
    WriteConsoleOutput(hOutput, screenBuffer, bufferSize, zeroZero, &draw_rect);
}

bool move_player(CHAR_INFO screenBuffer[]) {
     INPUT_RECORD InputRecord;
     DWORD Events=1;
     HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
     
     int old_p_posX = p1.posX; int old_p_posY = p1.posY;
     bool bPlayerMoved; int movemapdir;
     
     ReadConsoleInput(hInput, &InputRecord, 1, &Events);
     
     if(InputRecord.EventType == KEY_EVENT) {
         if(InputRecord.Event.KeyEvent.wVirtualKeyCode && InputRecord.Event.KeyEvent.bKeyDown) {
             switch(InputRecord.Event.KeyEvent.wVirtualKeyCode) {
                 case VK_UP:
                     if(p1.posY > 0) {
                         indenty++;
                         p1.posY--;
                         movemapdir=1;
                         bPlayerMoved = true;
                     }
                     break;
                 case VK_DOWN:
                     if(p1.posY < (grid_height - 1)) {
                         indenty--;
                         p1.posY++;
                         movemapdir=2;
                         bPlayerMoved = true;
                     }
                     break;
                 case VK_RIGHT:
                     if(p1.posX < (grid_width - 1)) {
                         indentx--;
                         p1.posX++;
                         movemapdir=3;
                         bPlayerMoved = true;
                     }
                     break;
                 case VK_LEFT:
                     if(p1.posX > 0) {
                         indentx++;
                         p1.posX--;
                         movemapdir=4;
                         bPlayerMoved = true;
                     }
                     break;
                 case VK_ESCAPE:
                     exit(0);
                     break;
             }
         }
     }
     FlushConsoleInputBuffer(hInput);
     
     if(bPlayerMoved) {
         if(screenBuffer[p1.posX + p1.posY * grid_width].Char.AsciiChar != ''1'') {
             screenBuffer[old_p_posX + old_p_posY * grid_width].Char.AsciiChar = '' '';
             screenBuffer[old_p_posX + old_p_posY * grid_width].Attributes = 0;
             return true;
         }else{
             p1.posX = old_p_posX; p1.posY = old_p_posY;
             switch(movemapdir) {
                 case 1:
                     indenty--; break;
                 case 2:
                     indenty++; break;
                 case 3:
                     indentx++; break;
                 case 4:
                     indentx--; break;
             }
             return true;
         }
     }
     return false;
}
void clear_screen() {
    CHAR_INFO screenBuffer[80 * 25];
    SMALL_RECT draw_rect = {0, 0, 79, 24};
    COORD bufferSize = {80, 25};
    COORD zeroZero = {0, 0};
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    for(int y=0; y<25; y++) {
        for(int x=0; x<80; x++) {
            screenBuffer[x + y * 80].Char.AsciiChar = '' '';
            screenBuffer[x + y * 80].Attributes = 0;
        }
    }
    WriteConsoleOutput(hOutput, screenBuffer, bufferSize, zeroZero, &draw_rect);
}
 
Basically, what happens is, the game loads a map, called test.txt, and lets your player move around. The map scrolls as the player moves. However, if you scroll the map partially out the bottom of the console buffer, or partially out the right of the console buffer, everything disappears. This doesn''t happen with the left side of the console or the top of the console. Any idea whats wrong? Oh yeah and my map at the moment looks like this: 10 8 35 14 5 5 1111111111 1000000001 1000000001 1000000001 1000000001 1000000001 1000000001 1111111111 where the first two numbers are grid_width and grid_height, the second two are indents, and the third pair is player start position. If you save this map and then run the program, move the player up as far as you can, and the map will just dissappear, yet if you move the player back down again, the map will magically reappear again. What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!