Skaruts

RootConsole.py

Jul 18th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.27 KB | None | 0 0
  1. import sfml as sf
  2. from managers.database import Database as db
  3. from managers.fonts import FontManager as fm
  4. from utils.colors import *
  5.  
  6. # layers
  7. MAP = 0
  8. OBJ = 1
  9. GUI = 2
  10.  
  11. class RootConsole:
  12.     def init(s, w, h, cw, ch, use_ascii=True):
  13.         s.w, s.h            = w, h
  14.  
  15.         s.defaultBGColor    = TRANSP
  16.         s.defaultFGColor    = TRANSP
  17.         s.defaultAlpha      = 255
  18.         s.defaultGlyph      = 0 # 0 (empty), 4 (Diamond)
  19.  
  20.         s.CW = cw   # cell width
  21.         s.CH = ch   # cell height
  22.  
  23.         s.use_ascii = use_ascii
  24.         s.gfxFont   = None
  25.         s.textFont  = None
  26.  
  27.         s.bgs       = sf.VertexArray(sf.PrimitiveType.QUADS)    # background color layer
  28.         s.mapLayer  = sf.VertexArray(sf.PrimitiveType.QUADS)    # scenery layer
  29.         s.objLayer  = sf.VertexArray(sf.PrimitiveType.QUADS)    # objects layer (entities, etc)
  30.         s.uiLayer   = sf.VertexArray(sf.PrimitiveType.QUADS)    # ui layer
  31.         s.textLayer = sf.VertexArray(sf.PrimitiveType.QUADS)    # text Layer
  32.  
  33.         s.createBuffers()
  34.  
  35.     def toggleAscii(s, fonts):
  36.         s.use_ascii = not s.use_ascii
  37.         s.set_fonts(fonts)
  38.  
  39.     def set_fonts(s, fonts):
  40.         s.textFont  = fonts[0]
  41.         s.gfxFont = fonts[1] if s.use_ascii else fonts[2]
  42.         s.set_render_states()
  43.  
  44.     def set_render_states(s):
  45.     # load textures
  46.         s.gfxTexture    = sf.Texture.from_image(s.gfxFont)
  47.         s.textTexture   = sf.Texture.from_image(s.textFont)
  48.     # create render states
  49.         s.gfxState  = sf.RenderStates()
  50.         s.textState = sf.RenderStates()
  51.     # assign RenderStates a texture
  52.         s.gfxState.texture  = s.gfxTexture
  53.         s.textState.texture = s.textTexture
  54.  
  55.     def update(s):
  56.         pass    # TODO or not TODO...
  57.  
  58.     def draw(s, window):
  59.         if s.textFont is not None and s.gfxFont is not None:
  60.             window.draw(s.bgs)
  61.             window.draw(s.mapLayer, s.gfxState)
  62.             window.draw(s.objLayer, s.gfxState)
  63.             window.draw(s.uiLayer, s.gfxState)
  64.             window.draw(s.textLayer, s.textState)
  65.         else:
  66.             raise ValueError ("Can't render: missing Fonts and RenderStates.")
  67.  
  68.     def createBuffers(s):
  69.         s.bgs.resize((s.w*2) * s.h * 4)
  70.         s.mapLayer.resize(s.w * s.h * 4)
  71.         s.objLayer.resize(s.w * s.h * 4)
  72.         s.uiLayer.resize(s.w * s.h * 4)
  73.         s.textLayer.resize((s.w*2) * s.h * 4)
  74.  
  75.         for i in xrange(s.w):       ####  MAP / OBJ / UI LAYERS  ####
  76.             for j in xrange(s.h):
  77.                 INDEX = (i+j*s.w) * 4
  78.  
  79.             # Foreground vertices positioning
  80.                 s.mapLayer[INDEX + 0].position = sf.Vector2(  i   *s.CW ,    j   *s.CH )    # top left
  81.                 s.mapLayer[INDEX + 1].position = sf.Vector2( (i+1)*s.CW ,    j   *s.CH )    # top right
  82.                 s.mapLayer[INDEX + 2].position = sf.Vector2( (i+1)*s.CW ,   (j+1)*s.CH )    # bot right
  83.                 s.mapLayer[INDEX + 3].position = sf.Vector2(  i   *s.CW ,   (j+1)*s.CH )    # bot left
  84.                
  85.                 s.objLayer[INDEX + 0].position = sf.Vector2(  i   *s.CW ,    j   *s.CH )    # top left
  86.                 s.objLayer[INDEX + 1].position = sf.Vector2( (i+1)*s.CW ,    j   *s.CH )    # top right
  87.                 s.objLayer[INDEX + 2].position = sf.Vector2( (i+1)*s.CW ,   (j+1)*s.CH )    # bot right
  88.                 s.objLayer[INDEX + 3].position = sf.Vector2(  i   *s.CW ,   (j+1)*s.CH )    # bot left
  89.                
  90.                 s.uiLayer [INDEX + 0].position = sf.Vector2(  i   *s.CW ,    j   *s.CH )    # top left
  91.                 s.uiLayer [INDEX + 1].position = sf.Vector2( (i+1)*s.CW ,    j   *s.CH )    # top right
  92.                 s.uiLayer [INDEX + 2].position = sf.Vector2( (i+1)*s.CW ,   (j+1)*s.CH )    # bot right
  93.                 s.uiLayer [INDEX + 3].position = sf.Vector2(  i   *s.CW ,   (j+1)*s.CH )    # bot left
  94.             # Foreground texture coordinates
  95.                 s.resetTexCoords(s.mapLayer, INDEX)
  96.                 s.resetTexCoords(s.objLayer, INDEX)
  97.                 s.resetTexCoords(s.uiLayer , INDEX)
  98.             # Foreground colors
  99.                 s.setVerticesColor(s.mapLayer, INDEX, 4, s.defaultFGColor)
  100.                 s.setVerticesColor(s.objLayer, INDEX, 4, s.defaultFGColor)
  101.                 s.setVerticesColor(s.uiLayer , INDEX, 4, s.defaultFGColor)
  102.  
  103.        
  104.         for i in xrange(s.w*2):     ####  BACKGROUND / TEXT LAYERS  ####
  105.             for j in xrange(s.h):
  106.                 INDEX = (i+j*(s.w*2)) * 4
  107.             # Background colors
  108.                 s.setVerticesColor(s.bgs, INDEX, 4, s.defaultBGColor)
  109.             # Background vertices positioning
  110.                 s.bgs[INDEX + 0].position = sf.Vector2(  i   *s.CW/2    ,    j   *s.CH ) # top left
  111.                 s.bgs[INDEX + 1].position = sf.Vector2( (i+1)*s.CW/2    ,    j   *s.CH ) # top right
  112.                 s.bgs[INDEX + 2].position = sf.Vector2( (i+1)*s.CW/2    ,   (j+1)*s.CH ) # bot right
  113.                 s.bgs[INDEX + 3].position = sf.Vector2(  i   *s.CW/2    ,   (j+1)*s.CH ) # bot left
  114.             # Text vertices positioning
  115.                 s.textLayer[INDEX + 0].position = sf.Vector2(  i   *s.CW/2  ,    j   *s.CH ) # top left
  116.                 s.textLayer[INDEX + 1].position = sf.Vector2( (i+1)*s.CW/2  ,    j   *s.CH ) # top right
  117.                 s.textLayer[INDEX + 2].position = sf.Vector2( (i+1)*s.CW/2  ,   (j+1)*s.CH ) # bot right
  118.                 s.textLayer[INDEX + 3].position = sf.Vector2(  i   *s.CW/2  ,   (j+1)*s.CH ) # bot left
  119.             # Text texture coordinates
  120.                 s.resetTexCoords(s.textLayer, INDEX)
  121.             # Text foreground colors
  122.                 s.setVerticesColor(s.textLayer, INDEX, 4, s.defaultFGColor)
  123.  
  124.     def setVerticesColor(s, layer, index, num_verts, color):
  125.         for i in xrange(num_verts):
  126.             layer[index + i].color = color
  127.    
  128.     def resetTexCoords(s, layer, index):
  129.         for i in xrange(4):
  130.             layer[index + i].tex_coords = sf.Vector2(0, 0)
  131.  
  132.     def set_fg_color(s, fg):
  133.         color = fg if fg is not None else s.defaultFGColor
  134.         return color
  135.  
  136.     def set_bg_color(s, bg):
  137.         color = bg if bg is not None else s.defaultBGColor
  138.         return color
  139.  
  140.     def getLayer(s, layer):
  141.         if   layer == MAP:  return s.mapLayer
  142.         elif layer == OBJ:  return s.objLayer
  143.         elif layer == UI:   return s.uiLayer
  144.  
  145.     def draw_cell(s, layer, x, y, glyph=None, fg=None, bg=None):
  146.         layer = s.getLayer(layer)
  147.  
  148.         if  x >= 0 and x < db.get("grid_width") \
  149.         and y >= 0 and y < db.get("grid_height"):
  150.             INDEX  = ( x   + y*s.w     ) * 4
  151.             INDEX2 = ( x*2 + y*(s.w*2) ) * 4
  152.  
  153.             bg_color = s.set_bg_color(bg)                   # Background colors
  154.             s.setVerticesColor(s.bgs, INDEX2, 8, bg_color)  # (full tile)
  155.  
  156.             if glyph is not None:                           # Foreground tex coords
  157.                 fg_color = s.set_fg_color(fg)
  158.                 s.resetTexCoords(layer, INDEX)
  159.             else:
  160.                 glyph = s.defaultGlyph
  161.                 fg_color = s.set_fg_color(s.defaultFGColor)
  162.            
  163.             tu = (glyph % (s.gfxFont.width / s.CW)) * s.CW  # col
  164.             tv = (glyph / (s.gfxFont.width / s.CW)) * s.CH  # row
  165.  
  166.             layer[INDEX + 0].tex_coords = sf.Vector2(  tu       , tv        ) # top left
  167.             layer[INDEX + 1].tex_coords = sf.Vector2(  tu+s.CW  , tv        ) # top right
  168.             layer[INDEX + 2].tex_coords = sf.Vector2(  tu+s.CW  , tv+s.CH   ) # bot right
  169.             layer[INDEX + 3].tex_coords = sf.Vector2(  tu       , tv+s.CH   ) # bot left
  170.  
  171.             s.setVerticesColor(layer, INDEX, 4, fg_color)   # Foreground colors
  172.  
  173.     def draw_char(s, x, y, glyph=None, fg=None, bg=None):
  174.         if  x >= 0 and x < db.get("grid_width")*2 \
  175.         and y >= 0 and y < db.get("grid_height"):
  176.             INDEX = ( x + y * s.w*2 ) * 4
  177.             bg_color = s.set_bg_color(bg)                   # Background colors
  178.             s.setVerticesColor(s.bgs, INDEX, 4, bg_color)   # (half tile)
  179.            
  180.             if glyph is not None:                           # Foreground tex coords
  181.                 fg_color = s.set_fg_color(fg)
  182.                 s.resetTexCoords(s.textLayer, INDEX)
  183.             else:
  184.                 glyph = s.defaultGlyph
  185.                 fg_color = s.set_fg_color(s.defaultFGColor)
  186.  
  187.            
  188.             CW2 = s.CW/2
  189.             tu = ( glyph % (s.textFont.width / CW2 )) * CW2     # col
  190.             tv = ( glyph / (s.textFont.width / CW2 )) * s.CH    # row
  191.  
  192.             s.textLayer[INDEX + 0].tex_coords = sf.Vector2( tu      ,   tv          )   # top left
  193.             s.textLayer[INDEX + 1].tex_coords = sf.Vector2( tu+CW2  ,   tv          )   # top right
  194.             s.textLayer[INDEX + 2].tex_coords = sf.Vector2( tu+CW2  ,   tv+s.CH     )   # bot right
  195.             s.textLayer[INDEX + 3].tex_coords = sf.Vector2( tu      ,   tv+s.CH     )   # bot left
  196.            
  197.             s.setVerticesColor(s.textLayer, INDEX, 4, fg_color) # Foreground colors
  198.  
  199.     def clear_cell(s, layer, x, y):
  200.         layer = s.getLayer(layer)
  201.         if  x >= 0 and x < db.get("grid_width") \
  202.         and y >= 0 and y < db.get("grid_height"):
  203.             INDEX = ( x + y * s.w ) * 4
  204.             INDEX2 = ( x*2 + y * (s.w*2) ) * 4
  205.             s.setVerticesColor(s.bgs, INDEX2, 8, s.defaultBGColor)  # Background colors (full tile)
  206.             s.resetTexCoords(layer, INDEX)                          # Foreground texture coordinates
  207.             s.setVerticesColor(layer, INDEX, 4, s.defaultFGColor)   # Foreground colors
  208.  
  209.     def clear_char(s, x, y):
  210.         if  x >= 0 and x < db.get("grid_width")*2 \
  211.         and y >= 0 and y < db.get("grid_height"):
  212.             INDEX = ( x + y * s.w*2 ) * 4
  213.             s.setVerticesColor(s.bgs, INDEX, 4, s.defaultBGColor)       # Background colors (half tile)
  214.             s.resetTexCoords(s.textLayer, INDEX)                        # Foreground tex coords
  215.             s.setVerticesColor(s.textLayer, INDEX, 4, s.defaultFGColor) # Foreground colors
Advertisement
Add Comment
Please, Sign In to add comment