pygameとopenglを使ったひな形的なコードです。
・png画像表示
・日本語テキスト表示
・pngは透過色指定で透過(抜け色)
・画像/フォントを行列により回転
・アルファブレンディング
を行っています。したがって、pygameとpyopenglモジュールが必要です。
また、コードと同じフォルダに、以下のファイルを置いてください。
・test.png
(表示用。テキトーな画像でOK。縦横サイズともに2の倍数。256*256ぐらいがおすすめ。)
・ipag.ttf
(IPAフォント。どっかでダウンロードしてきてください。)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os,sys
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
def initialize(w,h):
#pygame init
pygame.init()
modelist = pygame.display.list_modes()
nextmode = [l for l in modelist if l[0]>=w and l[1]>=h]
bestx, besty = -1,-1
for l in nextmode:
if (bestx==-1 or bestx>=l[0]) and (besty==-1 or besty>=l[1]):
bestx, besty = l[0],l[1]
pygame.display.set_mode((w,h), pygame.OPENGL|pygame.DOUBLEBUF)
#opengl init
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity();
gluOrtho2D(0, w, h, 0)
glMatrixMode(GL_MODELVIEW)
glEnable(GL_TEXTURE_2D)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
def main():
#1, init
filename = "test.png"
font_filename="ipag.ttf"
clock = pygame.time.Clock(); fps = 60
initialize(640,480)
#2-1, load image(picture)
try:
surface = pygame.image.load(filename)
except:
print "file load error"; sys.exit()
surface.set_colorkey((0,0,255),pygame.RLEACCEL )
data = pygame.image.tostring(surface, "RGBA", True)
width, height = surface.get_size()
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, width, height, 0, GL_RGBA,GL_UNSIGNED_BYTE, data)
#gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA,GL_UNSIGNED_BYTE, data)
#2-2, load image(font)
if font_filename != "":
font = pygame.font.Font(font_filename,30)
font_surface = font.render(u"こんにちは、世界",True,(255,255,255,255))
font_data = pygame.image.tostring(font_surface,"RGBA",True)
font_width, font_height = font_surface.get_size()
font_texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, font_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, font_width, font_height, 0, GL_RGBA,GL_UNSIGNED_BYTE, font_data)
#3-1, make display list(picture)
displaylist = glGenLists(1)
glNewList(displaylist,GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, texture)
glBegin(GL_QUADS)
glTexCoord2f(0, 0); glVertex2f(0, height)
glTexCoord2f(0, 1); glVertex2f(0, 0)
glTexCoord2f(1, 1); glVertex2f(width, 0)
glTexCoord2f(1, 0); glVertex2f( width, height)
glEnd()
glEndList()
#3-2, make display list(font)
if font_filename != "":
font_displaylist = glGenLists(1)
glNewList(font_displaylist,GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, font_texture)
glBegin(GL_QUADS)
glTexCoord2f(0, 0); glVertex2f(0, font_height)
glTexCoord2f(0, 1); glVertex2f(0, 0)
glTexCoord2f(1, 1); glVertex2f(font_width, 0)
glTexCoord2f(1, 0); glVertex2f( font_width, font_height)
glEnd()
glEndList()
#4, begin event loop
rot=0
while True:
#4-1, matrix setting(picture)
glLoadIdentity()
glTranslate(width/2,height/2,0)
glRotate(rot,0,0,-1)
glTranslate(-width/2,-height/2,0)
#4-2, draw(picture)
glClear( GL_COLOR_BUFFER_BIT )
glColor4f(1.0, 1.0, 1.0, rot/360.0)
glCallList(displaylist)
#4-3, matrix setting(font)
glLoadIdentity()
glTranslate(300,200,0)
glRotate(-rot,0,0,-1)
glTranslate(-font_width/2,-font_height/2,0)
#4-4, draw(font)
if font_filename != "":
glColor4f(1.0, 1.0, 1.0, rot/360.0)
glCallList(font_displaylist)
pygame.display.flip()
#4-5, event
for e in pygame.event.get():
if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESC):
return
#4-6, wait
clock.tick(fps)
#other
rot=0 if rot >= 360 else rot+0.5
#delete texture and displaylist
glDeleteLists(displaylist,1)
glDeleteTextures(texture)
if __name__ == '__main__':
main()