Browse Source

Add pixel access for SDL1 & SDL2

Yann Weber 5 years ago
parent
commit
37882671a9
2 changed files with 146 additions and 9 deletions
  1. 29
    0
      sdl.asm
  2. 117
    9
      yaglitch.asm

+ 29
- 0
sdl.asm View File

@@ -6,10 +6,19 @@ Extern SDL_OpenAudio
6 6
 Extern SDL_PauseAudio
7 7
 Extern SDL_WaitEvent
8 8
 Extern SDL_GetError
9
+Extern SDL_CreateRGBSurface
10
+Extern SDL_LockSurface
11
+Extern SDL_UnlockSurface
12
+Extern SDL_UpperBlit
13
+%define SDL_BlitSurface SDL_UpperBlit
9 14
 
10 15
 %ifdef SDL1
11 16
 	%define SDL_QUIT 0x0C
17
+	%define SDL_DOUBLEBUF 0x40000000
18
+	%define SDL_HWSURFACE 0x1
19
+	Extern SDL_GetVideoSurface
12 20
 	Extern SDL_SetVideoMode
21
+	Extern SDL_Flip
13 22
 %endif
14 23
 %ifdef SDL2
15 24
 	; events
@@ -20,6 +29,10 @@ Extern SDL_GetError
20 29
 	%define SDL_WINDOWPOS_UNDEFINED SDL_WINDOWPOS_UNDEFINED_MASK | 0 ; display 0
21 30
 	; function
22 31
 	Extern SDL_CreateWindow
32
+
33
+	extern SDL_GetWindowSurface
34
+	extern SDL_memset
35
+	extern SDL_UpdateWindowSurface
23 36
 %endif
24 37
 %ifdef MIX_AUDIO
25 38
 	Extern SDL_MixAudio
@@ -61,3 +74,19 @@ ENDSTRUC
61 74
 		ISTRUC SDL_AudioSpec_STRUC
62 75
 %endmacro
63 76
 
77
+%macro MOV_surf_w 2
78
+	mov %1, [%2]
79
+	mov %1, [%1 + 16]
80
+%endmacro
81
+%macro MOV_surf_h 2
82
+	mov %1, [%2]
83
+	mov %1, [%1 + 20]
84
+%endmacro
85
+%macro MOV_surf_pitch 2
86
+	mov %1, [%2]
87
+	mov %1, [%1 + 24]
88
+%endmacro
89
+%macro MOV_surf_pixels 2
90
+mov %1, [%2]
91
+mov %1, [%1 + 32]
92
+%endmacro

+ 117
- 9
yaglitch.asm View File

@@ -108,6 +108,14 @@ section .bss
108 108
 
109 109
 	visu_data_buff: resb 0x100
110 110
 
111
+	%ifdef SDL2
112
+		visu_win: resq 1
113
+	%endif
114
+
115
+	visu_scr: resq 1
116
+
117
+	visu_surf: resq 1
118
+
111 119
 section .text
112 120
 global _start
113 121
 _start:
@@ -369,14 +377,18 @@ sdl_init:
369 377
 	mov rdi, audiospec_wanted
370 378
 	mov rsi, audiospec_recv
371 379
 	call SDL_OpenAudio
380
+
372 381
 	; video init 256*256 window
382
+	; init visu_scr : *screen
373 383
 	%ifdef SDL1
374 384
 		mov rdi, 256
375 385
 		mov rsi, 256
376 386
 		mov rdx, 32
377 387
 		;mov rcx, SDL_SWSURFACE
378
-		xor rcx, rcx
388
+		mov rcx, SDL_DOUBLEBUF
379 389
 		call SDL_SetVideoMode
390
+		call SDL_GetVideoSurface
391
+		mov [visu_scr], rax
380 392
 	%endif
381 393
 	%ifdef SDL2
382 394
 		mov rdi, window_title
@@ -386,7 +398,92 @@ sdl_init:
386 398
 		mov r8, 256
387 399
 		xor r9, r9
388 400
 		call SDL_CreateWindow
401
+		mov [visu_win], rax
402
+		mov rdi, rax
403
+		call SDL_GetWindowSurface
404
+		mov [visu_scr], rax
405
+	%endif
406
+
407
+	; init SDL surface according to SDL screen in visu_surf
408
+	xor rdi, rdi ; flasg
409
+	mov rsi, 256 ; width
410
+	mov rdx, 256 ; height
411
+	mov rcx, 32 ; 32 bits depth
412
+	mov r8, 0xff ; rmask
413
+	mov r9, 0xff00 ; gmask
414
+	mov rax, 0xff0000 ; bmask
415
+	push rax
416
+	push rax
417
+	shl rax, 8 ; amask
418
+	mov [rsp+8], rax
419
+	call SDL_CreateRGBSurface
420
+	mov [visu_surf], rax
421
+	test rax, rax
422
+	jz sdl_error
423
+	pop rcx
424
+	pop rcx
425
+	; RAZ surface & blit
426
+	push rax ; push surface addr
427
+	mov rdi, rax
428
+	call SDL_LockSurface
429
+
430
+	%ifdef SDL2
431
+		xor rdx, rdx
432
+		xor rcx, rcx
433
+		MOV_surf_w ecx, visu_surf
434
+		xor rax, rax
435
+		MOV_surf_pitch eax, visu_surf
436
+		mul ecx
437
+		MOV_surf_pixels rdi, visu_surf
438
+		xor rsi, rsi
439
+		;mov rsi, 0xFF ;white
440
+		mov rdx, rax
441
+		call SDL_memset ; seems to malloc the pixels
442
+	%endif
443
+
444
+	%ifdef SDL1
445
+		xor rcx, rcx
446
+		MOV_surf_w ecx, visu_surf
447
+		push rcx
448
+		MOV_surf_h ecx, visu_surf
449
+		MOV_surf_pixels rdi, visu_surf
450
+		;mov rax, 0xffcc22ff ; magenta
451
+		;mov rax, 0xffffffff ; white
452
+		dec rcx
453
+		.loop_razh:
454
+			push rcx
455
+			mov rcx, [rsp+8]
456
+			dec rcx
457
+			.loop_razw:
458
+				stosd
459
+				loop .loop_razw
460
+			pop rcx
461
+			loop .loop_razh
462
+		pop rcx
389 463
 	%endif
464
+
465
+	pop rdi ; surface addr
466
+	call SDL_UnlockSurface
467
+
468
+	mov rdi, [visu_surf]
469
+	xor rsi, rsi
470
+	mov rdx, [visu_scr]
471
+	xor rcx, rcx
472
+	call SDL_BlitSurface
473
+	test rax, rax
474
+	jnz sdl_error
475
+
476
+	%ifdef SDL2
477
+		mov rdi, [visu_win]
478
+		call SDL_UpdateWindowSurface
479
+	%endif
480
+	%ifdef SDL1
481
+		mov rdi, [visu_scr]
482
+		call SDL_Flip
483
+	%endif
484
+
485
+
486
+
390 487
 	%ifdef MIX_AUDIO
391 488
 		; init callback heap infos
392 489
 		mov rax, 0xc ; brk
@@ -441,19 +538,34 @@ sdl_error:
441 538
 	; display error & exit
442 539
 	call SDL_GetError
443 540
 	push rax
541
+.dbg:
542
+	test rax, rax
543
+	jz .no_err
544
+	mov rdi, rax
444 545
 	call strlen
546
+	.printerr:
445 547
 	mov rdx, rsi ; msglen
446 548
 	pop rdi ; msg
447 549
 	mov rax, 1 ; write
448 550
 	mov rdi, 2 ; stderr
449 551
 	syscall
450 552
 	mov rdi, 0xF
451
-	jmp exit.exit_err
553
+	jmp sdl_exit_err
554
+	.no_err:
555
+	mov rdi, 0x5
556
+	jmp sdl_exit_err
452 557
 
453 558
 exit_fatal:
454 559
 	mov rdi, 42
455 560
 	jmp exit.exit_err
456 561
 
562
+sdl_exit_err:
563
+	push rdi
564
+	call SDL_Quit
565
+	pop rdi
566
+	mov rax, 0x3c
567
+	syscall
568
+
457 569
 exit:
458 570
 	call SDL_Quit
459 571
 
@@ -649,9 +761,7 @@ audio_cllbck:
649 761
 	; rdi -> *userdata
650 762
 	; rsi -> *stream
651 763
 	; rdx -> stream_len
652
-	%ifdef SDL2
653
-		push rbx ; strange, but nescessary...
654
-	%endif
764
+	push rbx
655 765
 	mov rcx, rdx
656 766
 	mov rdi, rsi
657 767
 	push rsi
@@ -674,10 +784,8 @@ audio_cllbck:
674 784
 	syscall
675 785
 	cmp rax, -1
676 786
 	je exit_fatal
677
-	%ifdef SDL2
678
-		pop rbx ; if rbx change SDL2 segfault :/
679
-	%endif
680
-		ret
787
+	pop rbx
788
+	ret
681 789
 %endif
682 790
 %ifdef MIX_AUDIO
683 791
 ; another version of the audio callback using heap to store the data

Loading…
Cancel
Save