Skip to content

Instantly share code, notes, and snippets.

@ricardoquesada
Last active May 26, 2016 03:42
Show Gist options
  • Save ricardoquesada/7049216 to your computer and use it in GitHub Desktop.
Save ricardoquesada/7049216 to your computer and use it in GitHub Desktop.
cocos2d-x v3.0 renderer pseudo code
#
# cocos2d-x v3.0 renderer
#
# for further info read: https://docs.google.com/document/d/17zjC55vbP_PYTftTZEuvqXuMb9PbYNxRFu0EGTULPK8/edit#heading=h.3kpkd5ktk6p1
#
# Populates the VBO.
# It stops if the command is a group or if
# the VBO's capacity is not enough for the current Quad Command
# commands: list of commands
# returns: command that needs to be parsed next
def populate_vbo( ctx ):
# only iterate from the previous command to the last one
range = ctx.get_commands[ctx.last_command] : ctx.get_commands[-1]
for command in range:
if command.is_group():
ctx.last_command = command
break
if command.is_quad():
if vbo.enough_capacity( command.quad_size() )
vbo.append( command.get_quads )
else
ctx.last_command = command
break
# Creates a new context for a new group command
def new_context( command ):
ctx.commands = command.get_commands()
ctx.set_gl_context = command.set_gl_context
ctx.unset_gl_context = command.unset_gl_context
ctx.last_command = 0
ctx.firt_time = True
return ctx
# Main function
# Parses, sorts, etc.
# ctx: renderer context
def parse_commands( ctx ):
# draws elements that weren't drawn
if ctx.first_time:
global_vbo.flush()
ctx.set_gl_context()
sort( ctx.get_commands() )
ctx.first_time = False
populate_vbo( ctx )
for command in ctx.get_commands():
# VBO full ?
if command == ctx.last_command
parse_commands( ctx )
elif command.is_group():
# start new recursion
parse_commands( new_context( command ) )
# Start from last point
parse_commands( ctx )
# Render is optimized for Quads
elif command.is_quad():
if ctx.current_material_id != command.get_material_id():
ctx.current_material_id = command.get_material_id()
command.set_material()
gl_draw( global_vbo, ctx.last_vbo_index, ctx.current_vbo_index )
ctx.last_vbo_index = ctx.current_vbo_index
ctx.unset_gl_context()
#
# Main entry.
# It just calls the recursive fuction
#
def renderer_start():
start = RenderCommandGroup( main_list, glClear, None)
parse_commands(new_context(start))
global_vbo.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment