Source

BASIC-RoBots / src / surface.py

##
## surface.py for BASIC-RoBots in /home/surply_p
##
## Copyright (C) 2012 Pierre Surply
## <pierre.surply@gmail.com>
##
## This file is part of BASIC-RoBots.
##
##    BASIC-RoBots is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    BASIC-RoBots is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
##
## Started on  Thu Jun 28 13:23:22 2012 Pierre Surply
## Last update Sun Sep  2 20:06:12 2012 Pierre Surply
##

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *


def surface2texture(surface): 
    texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, texture)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, \
                     surface.get_width(), \
                     surface.get_height(), \
                     0, GL_RGBA, GL_UNSIGNED_BYTE, \
                     pygame.image.tostring(surface, "RGBA", 1))
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    return texture

def cut_surface(surface, (w, h)):
    l_surface = []
    for y in range(surface.get_height() / h):
        for x in range(surface.get_width() / w):
            new = pygame.Surface((w, h))
            new.blit(surface, (0, 0), pygame.Rect(x*w, y*h, w, h))
            new.set_colorkey(pygame.Color(255, 0, 255, 255))
            l_surface.append(surface2texture(new))
    return l_surface
    
def change_color(surface, front, back = pygame.Color(255, 0, 255, 255)):
    new_surface = surface.copy()
    for y in range(new_surface.get_height()):
        for x in range(new_surface.get_width()):
            c = tuple(new_surface.get_at((x, y)))
            if (c == (255, 255, 255, 255)):
                new_surface.set_at((x, y), front)
            elif (c == (255, 0, 255, 255)):
                new_surface.set_at((x, y), back)
    return new_surface
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.