-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanami.py
147 lines (114 loc) · 4.34 KB
/
manami.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
import os
import sys
import atexit
from ctypes import *
import sdl2.ext
from sdl2 import *
from sdl2.sdlimage import *
from sdl2.sdlmixer import *
def apple_saved_state_disabler_hack(identifier):
"""Prevent Mac OS >= 10.7 to restore windows state
https://github.com/VisTrails/VisTrails/blob/master/vistrails/run.py
"""
import shutil
import platform
ss_base_path = '~/Library/Saved Application State'
defaults_cmd_1 = '/usr/bin/defaults write %s ApplePersistenceIgnoreState YES'
defaults_cmd_2 = '/usr/bin/defaults write %s NSQuitAlwaysKeepsWindows -bool false'
if platform.system() == 'Darwin':
release = platform.mac_ver()[0].split('.')
if len(release) >= 2:
major = int(release[0])
minor = int(release[1])
if major * 10 + minor >= 107:
ss_path = os.path.expanduser(
os.path.join(ss_base_path, identifier + '.savedState'))
if os.path.exists(ss_path):
shutil.rmtree(ss_path, ignore_errors=True)
os.system(defaults_cmd_1 % identifier) # esta es la clave!
os.system(defaults_cmd_2 % identifier)
def get_base_path():
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.abspath(os.environ['RESOURCEPATH']) # para py2app!
else:
# The application is not frozen
datadir = os.path.dirname(os.path.abspath(__file__))
return datadir
BASE_PATH = get_base_path()
RESOURCES = dict()
RESOURCES['gfx'] = sdl2.ext.Resources(BASE_PATH, 'gfx')
RESOURCES['sfx'] = sdl2.ext.Resources(BASE_PATH, 'sfx')
# igual que en setup.py (para py2app)
IDENTIFIER = 'com.creepypanda.games.manami'
def manami_main():
# callback para el cierre de aplicacion
def close_all_things():
Mix_FreeMusic(music)
SDL_DestroyTexture(texture)
SDL_DestroyRenderer(renderer)
SDL_DestroyWindow(window)
Mix_CloseAudio()
SDL_Quit()
# registramos el callback ahora
atexit.register(close_all_things)
# corregimos pifia al levantar ventana de python en OSX >= 10.7
apple_saved_state_disabler_hack(IDENTIFIER)
# 0 - VARIABLES GLOBALES
gameloop = True
window = pointer(SDL_Window())
renderer = pointer(SDL_Renderer())
texture = pointer(SDL_Texture())
music = pointer(Mix_Music())
rectangle = SDL_Rect()
event = SDL_Event()
# 1 - INICIAR EL JUEGO
# 1.1 - SDL
if 0 != SDL_Init(SDL_INIT_EVERYTHING):
sys.exit(SDL_GetError())
if 0 != SDL_CreateWindowAndRenderer(1080, 720,
SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL,
pointer(window), pointer(renderer)):
sys.exit(SDL_GetError())
# 1.2 - SDL_IMAGE
if 0 == IMG_Init(IMG_INIT_PNG):
sys.exit(IMG_GetError())
texture = IMG_LoadTexture(
renderer, RESOURCES['gfx'].get_path('manami_logo.png').encode())
if texture is None:
sys.exit(IMG_GetError())
w, h = c_int(0), c_int(0)
SDL_QueryTexture(texture, None, None, byref(w), byref(h))
rectangle.x, rectangle.y = 0, 0
rectangle.w, rectangle.h = w, h
# 1.3 - SDL_MIXER
if 0 != Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096):
sys.exit(Mix_GetError())
music = Mix_LoadMUS(RESOURCES['sfx'].get_path('bg_music.ogg').encode())
if music is None or 0 != Mix_PlayMusic(music, -1):
sys.exit(Mix_GetError())
# levantamos la ventana justo antes de comenzar el gameloop
SDL_SetWindowTitle(
window, b'Manami - Simple Game Skeleton for Python/SDL2')
SDL_SetWindowPosition(
window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED)
SDL_ShowWindow(window)
SDL_RaiseWindow(window)
# 2 - BUCLE DE JUEGO
while gameloop:
# 2.1 - PROCESA LA ENTRADA
while 0 != SDL_PollEvent(byref(event)):
if event.type == SDL_QUIT:
gameloop = False
break
# 2.2 - ACTUALIZAR EL ESTADO DEL JUEGO
# 2.3 - RENDERIZAR EL JUEGO
SDL_RenderClear(renderer)
SDL_RenderCopy(renderer, texture, None, byref(rectangle))
SDL_RenderPresent(renderer)
SDL_Delay(10)
# 3 - FINALIZAR EL JUEGO
# usamos atexit para llamar al callback que cierra todo
if __name__ == "__main__":
manami_main()