3

I have had the dream of making a Roguelike for a few months now, and for some reason, my stubborn mind won't let me use a library. How would one draw the map and manipulate it without using and sort of library other than stdio.h?

user1657155
  • 41
  • 1
  • 2

4 Answers4

4

C doesn't know what a "keyboard" or a "terminal screen" are. (Strangely as it seems, C does know what some things are, like "locales", but let's just not talk about that.) This means that any written functionality for these sort of devices will be inherently unportable.

Nonetheless, while you'll be able to make some pragmatic assumptions in your code -- for example, assuming that stdin is your keyboard and stdout is your screen --, you won't be able to properly control the cursor position and text coloring with standard text streams. In Linux, it is possible to control both using terminal escape codes, but in Windows this is not possible -- you will have to use console functions for that. Also, in Linux, you still will have to care about terminal buffering and other complications. Lastly, terminal capabilities differ somewhat considerably -- a typical Linux terminal has more capabilities than the Windows console, which can't display, for example, text in bold or italic.

In short, standard text streams are not suitable for creating interactive applications. The best solution would be to create a small layer which isolates the terminal handling implementation, in such a manner that you can later on decide how to best tackle the problem -- for example, using ncurses or implementing your own terminal handling code.

alecov
  • 4,882
  • 2
  • 29
  • 55
2
  1. Write your own version of ncurses (and termcap).
  2. Write your Roguelike.

Really, ncurses was specifically written to allow the graphics of a roguelike game.

If you want to do that without using any existing libraries you will end up slowly reimplementing the libraries.

lod
  • 1,098
  • 10
  • 13
  • 2
    "ncurses was specifically written to allow the graphics of a roguelike game." To be absolutely clear; CURSES is based on code written for the original *Rogue*. – Textmode Aug 28 '13 at 08:27
1

I would not recommend writing a roguelike in C without any libraries. Libraryless C is very weak for anything that requires graphical control.

There is a great wiki for roguelike development at http://roguebasin.roguelikedevelopment.org/index.php?title=Main_Page with many pages on algorithms and roguelike design. It also hosts many roguelikes, some will be open source, some will be in C. I recommend you read, try to reproduce in your own program, learn, read, try to reproduce in your own program, learn, etc on loop. And definitely use whatever libraries it recommends (likely some version of curses or ncurses, which is what console roguelikes normally but not always use)

Patashu
  • 21,443
  • 3
  • 45
  • 53
0

It depends on the degree you want.

  1. How to draw map if it's OK to use Windows API.

    • prepare map

      int map[15][20] = {
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
           {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
      
    • prepare printing function

      //used no double buffering and used putchar to make it simple
      void printAt(char c, int x, int y)
      {
          HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
          COORD p = {x,y};
          SetConsoleCursorPosition(h,p);
          putchar(c);
      }
      
    • include header

      #include <windows.h>
      #include <stdio.h>
      
    • make main loop

      int x = 10;
      int y = 10;
      //main loop
      while(1)
      {
          for(int cy=0;cy<15;cy++)
          {
              for(int cx=0;cx<20;cx++)
              {
                  int cell = map[cy][cx];
                 if(cell == 0)
                      printAt('.',cx,cy);//grass
                  else if(cell == 1)
                      printAt('T',cx,cy);//tree
                  else if(cell == 2)
                      printAt('?',cx,cy);//item
              }
          }
          printAt('@',x,y);//hero
          Sleep(500);
          x++;//simulate keypress
      }
      

      hero walk away from forest

  2. If you want to avoid Operating System API too, write to memory area called frame buffer to print character at certain location. And read memory area called keyboard ring buffer where pressed keys are stored. But if you want this approach, you may want to setup environment often called bare metal.

Words with bold font are keywords.

You may want to research those keywords if you want more.

Taeyun
  • 211
  • 2
  • 16