-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.py
More file actions
executable file
·70 lines (70 loc) · 2.12 KB
/
csv.py
File metadata and controls
executable file
·70 lines (70 loc) · 2.12 KB
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
#!/usr/bin/python3
#This is a level viewer that takes a CSV spreadsheet with unquoted text and draws it with PySDL2.
# Copyright (C) 2018 Sloan Gardner
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
from sdl2 import *
from sdl2.sdlimage import *
import sys
import ctypes
def bye():
IMG_Quit()
SDL_Quit()
def check(hope,fn):
if hope:
print('%s: OK!'%fn)
else:
print('%s: Not OK! %s'%(fn,SDL_GetError().decode('UTF-8')))
bye()
sys.exit(1)
def csvparse(f):
return [x.split(',') for x in f.split('\n')]
def loop():
for y,i in enumerate(csv):
for x,j in enumerate(i):
s=spr[j]
w=s.contents.w
h=s.contents.h
SDL_BlitSurface(s,None,fb,SDL_Rect(x*w,y*h,w,h))
SDL_UpdateWindowSurface(win)
ev=SDL_Event()
while SDL_PollEvent(ctypes.byref(ev)):
if ev.type==SDL_QUIT:
bye()
sys.exit(0)
def main():
global win,fb,spr,csv
check(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS)==0,'SDL_Init')
check(IMG_Init(IMG_INIT_PNG),'IMG_Init')
try:
csv=csvparse(open('level.csv').read())
except ex:
print('Load and Parse Level: Not OK! %s'%ex)
bye()
sys.exit(1)
spr={}
for i in csv:
for j in i:
if j not in spr:
spr[j]=IMG_Load(bytes('assets/'+j+'.png','UTF-8'))
check(spr[j],'IMG_Load')
first=list(spr.values())[0]
win=SDL_CreateWindow(b'CSV Level Viewer',SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,first.contents.w*len(csv[0]),first.contents.h*len(csv),0)
check(win,'SDL_CreateWindow')
fb=SDL_GetWindowSurface(win)
check(fb,'SDL_GetWindowSurface')
while True:
loop()
if __name__=='__main__':
main()