Skip to content

Commit

Permalink
Integración de gtk + spyral + sugargame + gstreamer
Browse files Browse the repository at this point in the history
  • Loading branch information
fdanesse committed Dec 6, 2014
1 parent 81d4157 commit 1d613af
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 87 deletions.
54 changes: 46 additions & 8 deletions GameView.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

import os
from signal import SIGTERM
import sys
sys.path.insert(1, "Lib/")

import gtk

from Globales import COLORES

import subprocess
import gobject
import sugargame2
import sugargame2.canvas
import spyral
import pygame
from Games.ug1.runme import Escena


class GameView(gtk.EventBox):

Expand All @@ -39,16 +43,50 @@ def __init__(self):
self.modify_bg(gtk.STATE_NORMAL, COLORES["toolbar"])
self.set_border_width(4)

self.add(gtk.Label("Juego 1"))
self.game = None
self.game = False
self.pump = False

self.pygamecanvas = sugargame2.canvas.PygameCanvas(self)
self.pygamecanvas.set_flags(gtk.EXPAND)
self.pygamecanvas.set_flags(gtk.FILL)

self.add(self.pygamecanvas)

self.connect("size-allocate", self.__reescalar)
self.show_all()

def __reescalar(self, widget, event):
if self.game:
rect = self.get_allocation()
print "FIXME: El juego debe reescalarse a", rect.width, rect.height

def __run_game(self):
rect = self.get_allocation()
spyral.director.init((rect.width, rect.height),
fullscreen=False, max_fps=30)
self.game = Escena(self)
spyral.director.push(self.game)
if self.pump:
gobject.source_remove(self.pump)
self.pump = False
self.pump = gobject.timeout_add(300, self.__pump)
spyral.director.run(sugar=True)

def __pump(self):
pygame.event.clear()
return True

def stop(self):
if self.pump:
gobject.source_remove(self.pump)
self.pump = False
if self.game:
os.kill(self.game, SIGTERM)
spyral.quit()
del(self.game)
self.game = False
self.hide()

def run(self, topic):
p = subprocess.Popen("python2 Games/ug1/runme.py", shell=True)
self.game = p.pid
self.pygamecanvas.run_pygame(self.__run_game)
self.pygamecanvas.grab_focus()
self.show()
8 changes: 4 additions & 4 deletions Games/ug1/runme.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/bin/env python2
# *-* coding: utf-8 *-*

import os
import sys
sys.path.insert(1, "../../Lib/")

game_dir = os.path.abspath(os.path.dirname(__file__))
os.chdir(game_dir)

import pygame
import spyral
import os
import random
import csv

SIZE = (700, 700)
TILE = (64, 64)

game_dir = os.path.abspath(os.path.dirname(__file__))


def gamedir(archivo):
return os.path.join(game_dir, archivo)

Expand Down
41 changes: 22 additions & 19 deletions Lib/spyral/director.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def pop():

def push(scene):
"""
Place *scene* on the top of the stack, and move control to it. This does
return control, so remember to return immediately after calling it.
Place *scene* on the top of the stack, and move control to it. This does
return control, so remember to return immediately after calling it.
:param scene: The new scene.
:type scene: :class:`Scene <spyral.Scene>`
Expand Down Expand Up @@ -199,23 +199,26 @@ def update_callback(delta):
A closure for handling events, which includes firing the update
related events (e.g., pre_update, update, and post_update).
"""
global _tick
if sugar:
while gtk.events_pending():
gtk.main_iteration()
if len(pygame.event.get([pygame.VIDEOEXPOSE])) > 0:
scene.redraw()
scene._handle_event("director.redraw")

scene._event_source.tick()
events = scene._event_source.get()
for event in events:
scene._queue_event(*spyral.event._pygame_to_spyral(event))
scene._handle_event("director.pre_update")
scene._handle_event("director.update",
spyral.Event(delta=delta))
_tick += 1
scene._handle_event("director.post_update")
try:
global _tick
if sugar:
while gtk.events_pending():
gtk.main_iteration()
if len(pygame.event.get([pygame.VIDEOEXPOSE])) > 0:
scene.redraw()
scene._handle_event("director.redraw")

scene._event_source.tick()
events = scene._event_source.get()
for event in events:
scene._queue_event(*spyral.event._pygame_to_spyral(event))
scene._handle_event("director.pre_update")
scene._handle_event("director.update",
spyral.Event(delta=delta))
_tick += 1
scene._handle_event("director.post_update")
except:
pass
clock.frame_callback = frame_callback
clock.update_callback = update_callback
clock.tick()
12 changes: 8 additions & 4 deletions Lib/spyral/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import spyral
import copy


def _new_spyral_surface(size):
"""
Internal method for creating a new Spyral-compliant Pygame surface.
"""
return pygame.Surface((int(size[0]),
int(size[1])),
pygame.SRCALPHA, 32).convert_alpha()
try:
return pygame.Surface((int(size[0]),
int(size[1])), pygame.SRCALPHA, 32).convert_alpha()
except:
return False


def from_sequence(images, orientation="right", padding=0):
"""
Expand Down Expand Up @@ -166,7 +170,7 @@ class Image(object):
filename.
:type size: :class:`Vec2D <spyral.Vec2D>`
:param str filename: If filename is set, the file with that name is loaded.
The appendix has a list of the
The appendix has a list of the
:ref:`valid image formats<ref.image_formats>`. If you do
not specify a filename, you *must* pass in a size.
Expand Down
20 changes: 13 additions & 7 deletions Lib/spyral/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
except ImportError:
spyral.exceptions.actors_not_available_warning()
_GREENLETS_AVAILABLE = False

from itertools import chain
from layertree import _LayerTree
from collections import defaultdict
Expand Down Expand Up @@ -100,7 +100,7 @@ def __init__(self, size = None, max_ups=None, max_fps=None):
spyral.event.register('director.update', self._handle_events,
scene=self)
if _GREENLETS_AVAILABLE:
spyral.event.register('director.update', self._run_actors,
spyral.event.register('director.update', self._run_actors,
('delta',), scene=self)
spyral.event.register('spyral.internal.view.changed',
self._invalidate_views, scene=self)
Expand Down Expand Up @@ -270,7 +270,7 @@ def _handle_events(self):
self._handle_event(type, event)
self._events = self._pending
self._pending = []

def _unregister_sprite_events(self, sprite):
for name, handlers in self._handlers.items():
self._handlers[name] = [h for h in handlers
Expand All @@ -295,7 +295,7 @@ def _unregister(self, event_namespace, handler):
in self._handlers[event_namespace]
if ((not isinstance(h[0], WeakMethodBound) and handler != h[0])
or (isinstance(h[0], WeakMethodBound)
and ((h[0].func is not handler.im_func)
and ((h[0].func is not handler.im_func)
or (h[0].weak_object_ref() is not handler.im_self))))]
if not self._handlers[event_namespace]:
del self._handlers[event_namespace]
Expand Down Expand Up @@ -584,7 +584,7 @@ def _draw(self):
# This function sits in a potential hot loop
# For that reason, some . lookups are optimized away
screen = self._surface

# First we test if the background has been updated
if self._background_version != self._background_image._version:
self._set_background(self._background_image)
Expand All @@ -595,7 +595,10 @@ def _draw(self):
for i in self._clear_this_frame + self._soft_clear:
i = x.clip(i)
b = self._background.subsurface(i)
screen.blit(b, i)
try:
screen.blit(b, i)
except:
return

# Now, we need to blit layers, while simultaneously re-blitting
# any static blits which were obscured
Expand All @@ -617,7 +620,10 @@ def _draw(self):
# as they are drawn and then no longer cleared
soft_clear = self._soft_clear
self._soft_clear = []
screen_rect = screen.get_rect()
try:
screen_rect = screen.get_rect()
except:
return
drawn_static = 0

blit_flags_available = pygame.version.vernum < (1, 8)
Expand Down
23 changes: 15 additions & 8 deletions Lib/spyral/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ def scale_surface(s, target_size):
int(math.ceil(target_size[1])))
if new_size == s.get_size():
return s
t = pygame.transform.smoothscale(s, new_size,
spyral.image._new_spyral_surface(new_size))
return t
try:
t = pygame.transform.smoothscale(s, new_size,
spyral.image._new_spyral_surface(new_size))
return t
except:
return False


class _Blit(object):
"""
Expand Down Expand Up @@ -154,10 +158,13 @@ def finalize(self):
Performs all the final calculations for this blit and calculates the
rect.
"""
self.surface = scale_surface(self.surface, self.final_size)
self.surface = self.surface.subsurface(self.area._to_pygame())
self.rect = pygame.Rect((self.position[0], self.position[1]),
self.surface.get_size())
try:
self.surface = scale_surface(self.surface, self.final_size)
self.surface = self.surface.subsurface(self.area._to_pygame())
self.rect = pygame.Rect((self.position[0], self.position[1]),
self.surface.get_size())
except:
pass

class _CollisionBox(object):
"""
Expand Down Expand Up @@ -196,4 +203,4 @@ def clip(self, rect):

def finalize(self):
self.rect = spyral.Rect(self.position, self.area.size)

Loading

0 comments on commit 1d613af

Please sign in to comment.