Jump to content

  • Log In with Google      Sign In   
  • Create Account

dmreichard

Member Since 24 Apr 2005
Offline Last Active Jun 27 2013 03:54 PM

Topics I've Started

[.net] C# Asynchronous Sockets

06 June 2011 - 09:58 AM

Hey all,

I was wondering what the preferred methods are for thread safety when working with the asynchronous socket/stream methods; more specifically what is the "right" way to block all other threads AFTER the EndRead method call returns so that a client's OnReceiveData() method doesn't clash with the main server thread and other client threads?

Thank you for your time,
David

Starting With OpenGL 3.x Using PyOpenGL

21 March 2011 - 07:51 PM

Hey Everyone,

I've been using pygame successfully for a good while now and I recently made the leap to trying and learning OpenGL. The hardest part has been trying to find good material that isn't bogged down by C++ helper libraries or such that I don't have access to (nor feel like writing a wrapper around, especially when learning a new technology.)

I decided that to proceed with any C++ book/tutorial on OpenGL 3.x using the programmable pipeline, I needed to write a program first that would display a triangle using non-deprecated functions. After hours of tinkering, this is my end result:

from OpenGL.GL import *
from OpenGL.arrays import vbo
from OpenGLContext.arrays import *
from OpenGL.GL.shaders import *

from numpy import array

import pygame
from pygame.constants import *

class TestContext(object):
    def __init__(self):   
        pygame.init()
        self.screen = pygame.display.set_mode((800, 600), OPENGL|DOUBLEBUF) 
        self.clock = pygame.time.Clock()
        
        VERTEX_SHADER = compileShader("""
        void main() {
            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        }""", GL_VERTEX_SHADER)
        FRAGMENT_SHADER = compileShader("""
        void main() {
            gl_FragColor = vec4(0, 1, 0, 1);
        }""", GL_FRAGMENT_SHADER)
        
        self.shader = compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
        
        self.vbo = vbo.VBO(
            array([
                [0, 1, 0],
                [-1, -1, 0],
                [1, -1, 0],
                [2, -1, 0],
                [4, -1, 0],
                [4, 1, 0],
                [2, -1, 0],
                [4, 1, 0],
                [2, 1, 0],
                ], 'f')
            )
        
    def Render(self):
        glUseProgram(self.shader)
        try:
            self.vbo.bind()
            try:
                glEnableClientState(GL_VERTEX_ARRAY)
                glVertexPointerf(self.vbo)
                glDrawArrays(GL_TRIANGLES, 0, 9)
            finally:
                self.vbo.unbind()
                glDisableClientState(GL_VERTEX_ARRAY)
        finally:
            glUseProgram(0)
            
    def Run(self):
        while True:
            time_passed = self.clock.tick(100)
            
            for event in pygame.event.get():
                if event.type == QUIT:
                    return
            pygame.display.set_caption("FPS: %.2f" % self.clock.get_fps())
            
            self.Render()
            pygame.display.flip()
            
if __name__ == "__main__":
    tc = TestContext()
    tc.Run()

This code works as intended, and displays a green triangle on the screen. I have the 5th edition of the OpenGL Super Bible, which uses C++ as the base language( that is fine ) but appears to use a C++ helper library for stock shaders, etc when beginning.

I guess my biggest questions are as follows:
1. Did I do this the right way? Is there any initialization code that I am missing?
2. After a whole day of searching I'm having a horrible time finding material and examples using non-deprecated functions. For instance, how do I achieve the same results that I would use glRotatef() and glTranslatef() for?

Finding good material on using the programmable pipeline has been hard enough, let alone finding material using Python which is my preferred language (however I know both Python and C/C++ and can transition just fine from one to the other) Any help would be much appreciated!

Thanks,
Dave

Desired Method for Updating Position Vector

22 January 2011 - 04:32 PM

Hey all,

I've recently been learning how to use the pygame SDL wrapper and brushing up on my linear algebra.. and was wondering what the better way to simulate movement would be.

Please Note:
self.pos is a vector describing the entity's current position
self.direction is a normalized vector
self.speed is speed in pixels per tick(or millisecond)
time_passed is milliseconds since last frame

Would I do this, based on the concept that speed * time = distance?
Use the angle (as a normalized vector) and multiply it by speed and time_passed...
    def update(self, time_passed):
        self.pos += self.direction * (self.speed * time_passed)


Or should I create velocity and acceleration vectors, and add the velocity to the original position to obtain the new position?
Note: Acceleration would be (I assume) calculated and added to the velocity, then cleared (based on acceleration not being preserved in Newtonian physics) every frame?
    def update(self, time_passed):
	self.pos += self.velocity * (1.0/time_passed)

Are either of these methods the way things are normally done in 2D/3D games?

Thanks,
David

Composition vs. Inheritance for Game Objects/Entities in a Game World

24 February 2010 - 03:42 AM

Hello all, As a side tinkering project I've been working on designing a MUD and how I want to represent individual entities. For those who are unaware of what a MUD is, please see the Wikipedia entry. After reading several articles and threads on the theory behind composite design for game entities, this one caught my interest. Unfortunately I felt as if it didn't have much of a closure and wanted to bring it back up. What I am mainly interested in is if anyone can provide links to or implementation details (preferably in C#) of a basic composite/component based design for game entities in order to hopefully help further my understanding of how it may work. From what I am understanding, a game object/entity class is composed of other classes which define characteristics and behavior. The concept seems simple enough, however my question(stemming from my current lack of experience in design) is how to make the objects communicate with each other in a "proper" way. Is communication handled by implementing an event system in which each behavior subclass would override some methods, such as: UnderAttack(), OnMove(), etc? I would greatly appreciate if anyone could provide further clarification on this , thank you!

Multiple Lua States for Each Entity, Good or Bad?

06 January 2009 - 07:19 AM

Hello everyone. I've recently been tinkering around with LuaInterface and decided to implement it into my rogue style text adventure I've been working on. The language I'm using is C#. To make a simple proof of concept I have an NPC which calls a "retaliation.lua" script every time he is attacked. It looks like this: if me.hp <= 20 then say("I'm out of here!") me:DoCommand("north") else me:DoCommand("attack " .. ch.Name) end Very simple. Everything works as expected, the NPC retaliates until their HP reaches 20 or under and then he flees north. Every time an NPC is created, I have a Lua object field which is assigned in the NPC constructor:
lua = new Lua();
lua.DoString("luanet.load_assembly(\"SimpleROGUE\")");
lua.DoString("luanet.import_type(\"SimpleROGUE.Player\")");
lua.RegisterFunction("say", this, this.GetType().GetMethod("Say"));
lua["me"] = this;

And the retaliation function:
public void Retaliate(Player player)
{
    lua["ch"] = player;
    lua.DoFile("scripts\\retaliation.txt");
}

(Yes, I know I should have a better approach instead of NPCs and the player sharing the same class. But I implemented NPCs and a basic attack command quickly for this proof of concept.) My main question, is this good practice to instantiate a new Lua object for every single entity in the game? Or should every entity share a single Lua object that is perhaps passed into the constructor? The one thing I like about this approach is the fact I can register each individual Say() function so that it only applies to the NPC being worked on. However, I'm not sure how efficient this approach is... if someone with more experience could give me some insight on this that would be great! If you require any more information / source code examples please let me know. -Dave

PARTNERS