Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sfml as sf
- from managers.database import Database as db
- from managers.fonts import FontManager as fm
- from utils.colors import *
- # layers
- MAP = 0
- OBJ = 1
- GUI = 2
- class RootConsole:
- def init(s, w, h, cw, ch, use_ascii=True):
- s.w, s.h = w, h
- s.defaultBGColor = TRANSP
- s.defaultFGColor = TRANSP
- s.defaultAlpha = 255
- s.defaultGlyph = 0 # 0 (empty), 4 (Diamond)
- s.CW = cw # cell width
- s.CH = ch # cell height
- s.use_ascii = use_ascii
- s.gfxFont = None
- s.textFont = None
- s.bgs = sf.VertexArray(sf.PrimitiveType.QUADS) # background color layer
- s.mapLayer = sf.VertexArray(sf.PrimitiveType.QUADS) # scenery layer
- s.objLayer = sf.VertexArray(sf.PrimitiveType.QUADS) # objects layer (entities, etc)
- s.uiLayer = sf.VertexArray(sf.PrimitiveType.QUADS) # ui layer
- s.textLayer = sf.VertexArray(sf.PrimitiveType.QUADS) # text Layer
- s.createBuffers()
- def toggleAscii(s, fonts):
- s.use_ascii = not s.use_ascii
- s.set_fonts(fonts)
- def set_fonts(s, fonts):
- s.textFont = fonts[0]
- s.gfxFont = fonts[1] if s.use_ascii else fonts[2]
- s.set_render_states()
- def set_render_states(s):
- # load textures
- s.gfxTexture = sf.Texture.from_image(s.gfxFont)
- s.textTexture = sf.Texture.from_image(s.textFont)
- # create render states
- s.gfxState = sf.RenderStates()
- s.textState = sf.RenderStates()
- # assign RenderStates a texture
- s.gfxState.texture = s.gfxTexture
- s.textState.texture = s.textTexture
- def update(s):
- pass # TODO or not TODO...
- def draw(s, window):
- if s.textFont is not None and s.gfxFont is not None:
- window.draw(s.bgs)
- window.draw(s.mapLayer, s.gfxState)
- window.draw(s.objLayer, s.gfxState)
- window.draw(s.uiLayer, s.gfxState)
- window.draw(s.textLayer, s.textState)
- else:
- raise ValueError ("Can't render: missing Fonts and RenderStates.")
- def createBuffers(s):
- s.bgs.resize((s.w*2) * s.h * 4)
- s.mapLayer.resize(s.w * s.h * 4)
- s.objLayer.resize(s.w * s.h * 4)
- s.uiLayer.resize(s.w * s.h * 4)
- s.textLayer.resize((s.w*2) * s.h * 4)
- for i in xrange(s.w): #### MAP / OBJ / UI LAYERS ####
- for j in xrange(s.h):
- INDEX = (i+j*s.w) * 4
- # Foreground vertices positioning
- s.mapLayer[INDEX + 0].position = sf.Vector2( i *s.CW , j *s.CH ) # top left
- s.mapLayer[INDEX + 1].position = sf.Vector2( (i+1)*s.CW , j *s.CH ) # top right
- s.mapLayer[INDEX + 2].position = sf.Vector2( (i+1)*s.CW , (j+1)*s.CH ) # bot right
- s.mapLayer[INDEX + 3].position = sf.Vector2( i *s.CW , (j+1)*s.CH ) # bot left
- s.objLayer[INDEX + 0].position = sf.Vector2( i *s.CW , j *s.CH ) # top left
- s.objLayer[INDEX + 1].position = sf.Vector2( (i+1)*s.CW , j *s.CH ) # top right
- s.objLayer[INDEX + 2].position = sf.Vector2( (i+1)*s.CW , (j+1)*s.CH ) # bot right
- s.objLayer[INDEX + 3].position = sf.Vector2( i *s.CW , (j+1)*s.CH ) # bot left
- s.uiLayer [INDEX + 0].position = sf.Vector2( i *s.CW , j *s.CH ) # top left
- s.uiLayer [INDEX + 1].position = sf.Vector2( (i+1)*s.CW , j *s.CH ) # top right
- s.uiLayer [INDEX + 2].position = sf.Vector2( (i+1)*s.CW , (j+1)*s.CH ) # bot right
- s.uiLayer [INDEX + 3].position = sf.Vector2( i *s.CW , (j+1)*s.CH ) # bot left
- # Foreground texture coordinates
- s.resetTexCoords(s.mapLayer, INDEX)
- s.resetTexCoords(s.objLayer, INDEX)
- s.resetTexCoords(s.uiLayer , INDEX)
- # Foreground colors
- s.setVerticesColor(s.mapLayer, INDEX, 4, s.defaultFGColor)
- s.setVerticesColor(s.objLayer, INDEX, 4, s.defaultFGColor)
- s.setVerticesColor(s.uiLayer , INDEX, 4, s.defaultFGColor)
- for i in xrange(s.w*2): #### BACKGROUND / TEXT LAYERS ####
- for j in xrange(s.h):
- INDEX = (i+j*(s.w*2)) * 4
- # Background colors
- s.setVerticesColor(s.bgs, INDEX, 4, s.defaultBGColor)
- # Background vertices positioning
- s.bgs[INDEX + 0].position = sf.Vector2( i *s.CW/2 , j *s.CH ) # top left
- s.bgs[INDEX + 1].position = sf.Vector2( (i+1)*s.CW/2 , j *s.CH ) # top right
- s.bgs[INDEX + 2].position = sf.Vector2( (i+1)*s.CW/2 , (j+1)*s.CH ) # bot right
- s.bgs[INDEX + 3].position = sf.Vector2( i *s.CW/2 , (j+1)*s.CH ) # bot left
- # Text vertices positioning
- s.textLayer[INDEX + 0].position = sf.Vector2( i *s.CW/2 , j *s.CH ) # top left
- s.textLayer[INDEX + 1].position = sf.Vector2( (i+1)*s.CW/2 , j *s.CH ) # top right
- s.textLayer[INDEX + 2].position = sf.Vector2( (i+1)*s.CW/2 , (j+1)*s.CH ) # bot right
- s.textLayer[INDEX + 3].position = sf.Vector2( i *s.CW/2 , (j+1)*s.CH ) # bot left
- # Text texture coordinates
- s.resetTexCoords(s.textLayer, INDEX)
- # Text foreground colors
- s.setVerticesColor(s.textLayer, INDEX, 4, s.defaultFGColor)
- def setVerticesColor(s, layer, index, num_verts, color):
- for i in xrange(num_verts):
- layer[index + i].color = color
- def resetTexCoords(s, layer, index):
- for i in xrange(4):
- layer[index + i].tex_coords = sf.Vector2(0, 0)
- def set_fg_color(s, fg):
- color = fg if fg is not None else s.defaultFGColor
- return color
- def set_bg_color(s, bg):
- color = bg if bg is not None else s.defaultBGColor
- return color
- def getLayer(s, layer):
- if layer == MAP: return s.mapLayer
- elif layer == OBJ: return s.objLayer
- elif layer == UI: return s.uiLayer
- def draw_cell(s, layer, x, y, glyph=None, fg=None, bg=None):
- layer = s.getLayer(layer)
- if x >= 0 and x < db.get("grid_width") \
- and y >= 0 and y < db.get("grid_height"):
- INDEX = ( x + y*s.w ) * 4
- INDEX2 = ( x*2 + y*(s.w*2) ) * 4
- bg_color = s.set_bg_color(bg) # Background colors
- s.setVerticesColor(s.bgs, INDEX2, 8, bg_color) # (full tile)
- if glyph is not None: # Foreground tex coords
- fg_color = s.set_fg_color(fg)
- s.resetTexCoords(layer, INDEX)
- else:
- glyph = s.defaultGlyph
- fg_color = s.set_fg_color(s.defaultFGColor)
- tu = (glyph % (s.gfxFont.width / s.CW)) * s.CW # col
- tv = (glyph / (s.gfxFont.width / s.CW)) * s.CH # row
- layer[INDEX + 0].tex_coords = sf.Vector2( tu , tv ) # top left
- layer[INDEX + 1].tex_coords = sf.Vector2( tu+s.CW , tv ) # top right
- layer[INDEX + 2].tex_coords = sf.Vector2( tu+s.CW , tv+s.CH ) # bot right
- layer[INDEX + 3].tex_coords = sf.Vector2( tu , tv+s.CH ) # bot left
- s.setVerticesColor(layer, INDEX, 4, fg_color) # Foreground colors
- def draw_char(s, x, y, glyph=None, fg=None, bg=None):
- if x >= 0 and x < db.get("grid_width")*2 \
- and y >= 0 and y < db.get("grid_height"):
- INDEX = ( x + y * s.w*2 ) * 4
- bg_color = s.set_bg_color(bg) # Background colors
- s.setVerticesColor(s.bgs, INDEX, 4, bg_color) # (half tile)
- if glyph is not None: # Foreground tex coords
- fg_color = s.set_fg_color(fg)
- s.resetTexCoords(s.textLayer, INDEX)
- else:
- glyph = s.defaultGlyph
- fg_color = s.set_fg_color(s.defaultFGColor)
- CW2 = s.CW/2
- tu = ( glyph % (s.textFont.width / CW2 )) * CW2 # col
- tv = ( glyph / (s.textFont.width / CW2 )) * s.CH # row
- s.textLayer[INDEX + 0].tex_coords = sf.Vector2( tu , tv ) # top left
- s.textLayer[INDEX + 1].tex_coords = sf.Vector2( tu+CW2 , tv ) # top right
- s.textLayer[INDEX + 2].tex_coords = sf.Vector2( tu+CW2 , tv+s.CH ) # bot right
- s.textLayer[INDEX + 3].tex_coords = sf.Vector2( tu , tv+s.CH ) # bot left
- s.setVerticesColor(s.textLayer, INDEX, 4, fg_color) # Foreground colors
- def clear_cell(s, layer, x, y):
- layer = s.getLayer(layer)
- if x >= 0 and x < db.get("grid_width") \
- and y >= 0 and y < db.get("grid_height"):
- INDEX = ( x + y * s.w ) * 4
- INDEX2 = ( x*2 + y * (s.w*2) ) * 4
- s.setVerticesColor(s.bgs, INDEX2, 8, s.defaultBGColor) # Background colors (full tile)
- s.resetTexCoords(layer, INDEX) # Foreground texture coordinates
- s.setVerticesColor(layer, INDEX, 4, s.defaultFGColor) # Foreground colors
- def clear_char(s, x, y):
- if x >= 0 and x < db.get("grid_width")*2 \
- and y >= 0 and y < db.get("grid_height"):
- INDEX = ( x + y * s.w*2 ) * 4
- s.setVerticesColor(s.bgs, INDEX, 4, s.defaultBGColor) # Background colors (half tile)
- s.resetTexCoords(s.textLayer, INDEX) # Foreground tex coords
- s.setVerticesColor(s.textLayer, INDEX, 4, s.defaultFGColor) # Foreground colors
Advertisement
Add Comment
Please, Sign In to add comment