Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

curses roguelike starter code

Name: Anonymous 2012-06-11 9:55

found an old roguelike tutorial that teaches the basics but its code was written for some simplified windows console library. i redid the tutorial using curses for a game im working on. here is the code if anyone wants a template to experiment with roguelike stuff.

#include <curses.h>

// Map dimensions
#define MAP_WIDTH  20
#define MAP_HEIGHT 15

// Tile definitions
#define TILE_FLOOR 0
#define TILE_WALL  1
#define TILE_GRASS 2
#define TILE_OPENDOOR 3

// Map array
int MapArray[MAP_HEIGHT][MAP_WIDTH] = {
  { 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, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
  { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

// Map drawing function
void DrawMap( void ) {
  for( int y = 0; y < MAP_HEIGHT; y++ ) {
   
    // Jump to next line
    move( y, 0 );
    for( int x = 0; x < MAP_WIDTH; x++ ) {
      switch( MapArray[y][x] ) {

        // Draw tiles
        case TILE_FLOOR:
          attron( COLOR_PAIR(3) );
          addch('.');
          attroff( COLOR_PAIR(3) );
        break;

        case TILE_WALL:
          addch('#');
        break;
      }
    }
  }
}

// Collision detect function
bool IsPassable( int MapX, int MapY ) {

  // Make area outside map impassible
  if( MapX < 0 || MapX >= MAP_WIDTH || MapY < 0 || MapY >= MAP_HEIGHT )
  return false;

  // Store the value of the tile specified
  int TileValue = MapArray[MapY][MapX];

  // Return true if it's passable
  if( TileValue == TILE_FLOOR || TileValue == TILE_GRASS || TileValue == TILE_OPENDOOR )
  return true;

  // If execution gets here, it's not passable
  return false;
}


int main( void ) {

  // Init curses
  keypad( initscr(), 1 );
  curs_set( 0 );

  // Init colors
  start_color();
  init_pair( 1, COLOR_RED, COLOR_BLACK );
  init_pair( 2, COLOR_YELLOW, COLOR_BLACK );
  init_pair( 3, COLOR_GREEN, COLOR_BLACK );
  init_pair( 4, COLOR_BLUE, COLOR_BLACK );

  // Init start position
  int PlayerX = 4;
  int PlayerY = 4;

  // Game Loop
  while( true ) {

    // Refresh screen
    refresh();

    // Draw map
    DrawMap();
   
    // Draw player
    attron( COLOR_PAIR(2) );
    mvaddch( PlayerY, PlayerX, '@' );
    attroff( COLOR_PAIR(2) );

    // Wait for input
    char PlayerKey = getch();

    // Process input
    switch( PlayerKey ) {

      case 'k': // Up
        if( IsPassable( PlayerX, PlayerY - 1 ) ) {
          PlayerY--;
        }
      break;

      case 'j': // Down
        if( IsPassable( PlayerX, PlayerY + 1 ) ) {
          PlayerY++;
        }
      break;

      case 'h': // Left
        if( IsPassable( PlayerX - 1, PlayerY ) ) {
          PlayerX--;
        }
      break;

      case 'l': // Right
        if( IsPassable( PlayerX + 1, PlayerY ) ) {
          PlayerX++;
        }
      break;

      case 'q': // Quit
      return endwin();
    }
  }
}

Name: mjk 2012-06-11 16:23

>>5
Really? What feature of DirectX 11 isn’t available with OpenGL 4.1 + CUDA or OpenCL?
NVIDIA’s OpenGL can inter-operate with buffers and textures for either CUDA or OpenCL or DirectX or even OptiX. DirectX 11 can only interop with DirectX buffers and surfaces. This seems like a situation where OpenGL is clearly MORE functional than Direct3D. You can look at the NV_gpu_program5, NV_shader_buffer_load, and ARB_texture_buffer_object extensions to see how an OpenGL shader can read, and yes, even STORE to buffers.
OpenGL has plenty of features not available in DirectX 11 such as line stipple, accumulation buffers, immediate mode, display lists, smooth lines, access to shaders through both high-level shading languages (Cg or GLSL) as well as assembly language (NV_gpu_program5, NV_gpu_program4, ARB_fragment_program, etc.). And this all comes with cross-operating system portability, something DirectX 11 will never offer.
For example, OpenGL 4.1′s entire feature set, including programmable geometry and tessellation shaders and all the other DirectX 10 and 11 features are available on Windows XP as well as Vista and Windows 7.
OpenGL 4.1 supports both OpenGL and Direct3D conventions. So in Direct3D, you are stuck with the Direct3D provoking vertex or shader window origin convention, but with OpenGL 4.1 you can now pick your convention.
NVIDIA OpenGL has lots of ways it surpasses DirectX 11 in functionality such as support for bindless graphics (NV_vertex_buffer_unified_memory & NV_shader_buffer_load), arbitrary texture format swizzling EXT_texture_swizzle), Serial Digital Interface (SDI) video support (NV_present_video) including video capture (NV_video_capture).
BTW, you can get the actual PowerPoint slides from here: http://www.slideshare.net/Mark_Kilgard/opengl-4-for-2010
(This slides are corrected and slightly expanded from the recorded presentation. For example, they include code for implementing adaptive PN triangles.)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List