;                                                              ;
;                Main file of Burial Gfx Editor v.3.o5         ;
;                            (2ooo-2oo1)                       ;
;                                                              ;
;                      Open Sources for PHAT01                 ;
;                          Official releaze                    ;
;                       (without Supreme-GUI)                  ;
;                                                              ;
; Assembler: Alasm 4.42                                        ;
; Computer : Kay 1o24 (64pages)                                ;
;                                                              ;
; (c) Copyright Delirium Tremens from Freedom                  ;
; (c) Copyright Sinn from Delirium Tremens                     ;
;                                                              ;

COMPILE EQU 1 ; flag 0 - for edit compilation
              ; flag 1 - for full compilation
;...............................................................
; Константы BurGE 3.xx
;
BeginGUI EQU #C800
SCREEN   EQU #C000
UNDOSCR  EQU #C000+6912
ScrPage  EQU 0
SuxPage  EQU 1
GUIPage  EQU 3
AddPage  EQU 4
PgnPage1 EQU 6
PgnPage2 EQU 7
RamPage  EQU 7
PgnAdr   EQU #A500
AddAdr   EQU #C000
Buffer   EQU #C000-#400
SCRSPR   EQU #C000
ATRSPR   EQU SCRSPR+6144
SCRBGE   EQU #C000+6912
A_PGN1   EQU #C000  ; адрес четного плагина
A_PGN2   EQU #DB00  ; адрес нечетного плагина
LEN_PGN  EQU 6912   ; макс. длина плагина
MINPAGE  EQU 255    ; мин. страница для BGE256
;...............................................................

        ORG #C000,GUIPage
-       INCBIN "gui2"

        ORG #6000
        INCLUDE "PGNJP",#C1

        ORG #6128
        INCLUDE "GuiVar",#40
        INCLUDE "ResGUI",#08
        INCLUDE "Disk1",#41
        INCLUDE "Coord1",#0E
ESA

ExtMem  DB 0               ; extend mem

RUN     LD (SPP),SP
        LD A,GUIPage+#10
        LD (GUI_PAGE),A
        LD A,SuxPage+#10
        LD (STORE_PAGE),A
        LD A,#10
        LD (S_PAGE),A
        LD HL,Resident
        LD (ResAdr),HL
; ---------------------------------------
; - предварительная настройка редактора -
; ---------------------------------------
        CALL AUTOCONF

        IF COMPILE-1
        XOR A
        CALL LPGN1
        XOR A
        LD (vext),A
        LD A,(ExtPage)
        CP 6+1
        JP C,noexpa
        LD A,#FF
        LD (vext),A
noexpa  CALL IniSup
        LD A,LOAD_CAT
        CALL GUI
        CALL LdSup
        LD A,(ExtPage)
        CP 6+1
        JR NC,okext
        XOR A
        LD (vext),A
        LD (ssup+4),A
        CALL IniSup
okext   CALL SAVEOBJ
        CALL LdGfx
        CALL Fldlst
        ENDIF 

        CALL CLS
        CALL SAVESCR
        CALL SAVESCR2
        CALL SV_UNDO
        IF COMPILE-1
        CALL Fldadd
        ENDIF 
; ---------------------------------------
Main    LD SP,0
SPP     EQU $-2

        CALL LOADSCR
main    CALL ERASE_ALLW
        CALL IniVar
        LD IX,MWin
        LD A,%1001
        LD (IX+6),A
        CALL Win_
        CP #FF
        JR Z,lookde2_
        LD BC,TBMain
        JP TAB_JP

lookde2_
        LD A,(BRK)
        BIT 1,A
        JP Z,lookde2
        LD IX,WC
        CALL POPUP
        CALL Win_
        CALL CLOSE_
        CP #FF
        JP Z,main
        LD BC,TBPup
        JP TAB_JP

ReWin   CALL LOADSCR
        CALL ERASE_ALLW
        CALL IniVar
        LD IX,MWin
        LD A,%1001
        LD (IX+6),A
        CALL Window_
        RET 

main_   CALL CLOSE_ALLW
        JP main

Main_SV CALL SAVESCR
        JP Main

lookdel CALL LOADSCR
        CALL NOKEY_
        JR Main

lookde2 CALL LOADSCR
        CALL NOKEY_
        CALL WAITKEY_
        CALL NOKEY_
        JR Main

TBPup   DW UNDO
        DW dldscr
        DW dsvscr
        DW EMgn
        DW ESolidE
        DW ESNAPE_
        DW EBox
        DW SCRAM

WC      DB 0,0,8,10,5*8,5,%1110,0
        DW Twc
        DW Swc
        DW #FFFF
Twc     DB "Корзина",13
        DB "Открыть",13
        DB "Сохранить",13
        DB "Лупа",13
        DB "Заливка",13
        DB "Примитив",13
        DB "Ячейки",13
        DB "Обменять",0
Swc     DB 1,1,6,1,%11
        DB 1,2,6,1,%11
        DB 1,3,6,1,%11
        DB 1,4,6,1,%11
        DB 1,5,6,1,%11
        DB 1,6,6,1,%11
        DB 1,7,6,1,%11
        DB 1,8,6,1,%11
        DB #FF   
;-----------------

IniVar  LD HL,ARWSPR
        LD (ARW_ADR),HL
        LD HL,Ret
        LD DE,Ret
        CALL SInt1
        LD HL,Main_IntP
        LD A,PUT_INT
        CALL GUI
        LD HL,Main_IntG
        LD A,GET_INT
        CALL GUI
        LD HL,COOR_N
        LD (COOR_P+1),HL
        LD A,ARW_OLD
        CALL GUI
        CALL NormSpd
        XOR A
        LD (ARWXMN),A
        LD (ARWYMN),A
        DEC A
        LD (ARWXMX),A
        LD A,191
        LD (ARWYMX),A
        RET 

NormSpd LD HL,STEPN
        LD DE,STEP
        LDI 
        LDI 
        LDI 
        RET 

STEPN   DEFB 6
LEVELN  DEFB 10
SPEEDN  DEFB 1

SInt1   LD (IntP1),HL
        LD (IntG1),DE
        RET 

TBMain  DW EDisk     ; 00
        DW EPgns
        DW EEXE256
        DW EChr
        DW EFnt
        DW EWin8
        DW EWin1
        DW ERGB
        DW EMgn
        DW ECnk
        DW EMask
        DW ERDisk
        DW EFill
        DW ESNAPE
        DW EBrush
        DW EBox
        DW EMisc
        DW UNDO
;       DW Exit_DOS  ; 16
        DW lookdel
        DW ESNAPE_

SUNDO   CALL SV_UNDO
        JP Main

Exit_DOS
        LD SP,(SPP)
        IF COMPILE
        RET 
        ENDIF 
        DI 
        LD SP,#5FFF
        XOR A
        CALL RAM
        AND A
        SBC HL,HL
        PUSH HL
        JP #3D21

EMisc   XOR A
        CALL LPGN1
        JP PgnAdr

ETekBox LD A,1
        CALL LPGN2
        JP Tek_Box

EBox    LD A,1
        JP exe2

ESolidE LD A,2
        CALL LPGN1
        JP ESolid

EChBut  LD HL,(manbut)
        CPHL0
        LD A,H
        OR L
        JR Z,chbut2
        AND A
        SBC HL,HL
chbut0  LD (manbut),HL
        JP Main
chbut2  LD HL,BUTTONS
        JR chbut0

ETFillE LD A,2
        CALL LPGN1
        CALL etfill
        JP main_

ETWashE LD A,2
        CALL LPGN1
        JP EWash

ETWashe LD A,2
        CALL LPGN1
        JP ewash_

EFill   LD A,2
exe     CALL LPGN1
        JP PgnAdr

ECnk    LD A,3
        JR exe

EMask   LD A,3
        CALL LPGN1
        JP BMask

CWin8   CALL SAVEOBJ
EWin8   LD A,4
        JR exe

EWin8_  LD A,4
        CALL LPGN1
        CALL SV_UNDO
        JP DEFINE8

CWin1   CALL SAVEOBJ
EWin1   LD A,5
        JP exe

EWin1_  LD A,5
        CALL LPGN1
        CALL SV_UNDO
        JP DEFINE1

EMgn    LD A,7
        JP exe

EChr    LD A,6
exe2    CALL LPGN2
        JP PgnAdr

LPGN2   LD B,PgnPage2
        JR lpgn

JBrush  LD A,9
        CALL LPGN2
        CALL BrsSel
        PUSH AF
        CALL CLOSE
        CALL SAVEOBJ
        POP AF
        JP C,main_
        CALL prtbrsK
        CALL NormSpd
        CALL SAVESCR
        LD A,ARW_OLD
        CALL GUI
        JP Main

EBrush  LD A,9
        JP exe2

EFnt    LD A,8
        JP exe2

ECOORD  LD A,10
        JP exe2

EHack   LD A,11
        JP exe2

LPGN1   LD B,PgnPage1
lpgn    PUSH BC
        ADD A,A
        ADD A,A
        LD BC,TBPgns
        LD H,0
        LD L,A
        ADD HL,BC
        DEFHL1
        PUSH DE
        DEFHL1
        PUSH DE
        POP BC
        POP HL
        LD DE,PgnAdr
        POP AF
        JP LOADOBJ

TBPgns  DW MiscPgn,LMisc
        DW BoxPgn,LBox
        DW FillPgn,LFill
        DW CnkPgn,LCnk
        DW WinPgn,LWin8
        DW WinP,LWin1
        DW ChrPgn,LChr
        DW MgnPgn,LMgn
        DW FntPgn,LFnt  ; [08]
        DW BrsPgn,LBrs  ; [09]
        DW CxyPgn,LCxy  ; [10]
        DW HackPgn,LHack ; [11]

M_MNU   LD IX,m_mnu
        LD A,(MWin)
        LD (IX+0),A
        LD A,(MWin+1)
        LD (IX+1),A
        XOR A
        CALL Ramka8
        JP C,main
@@@@    LD A,(IX+0)
        AND A
        JP NZ,main
        LD A,(IX+1)
        CP 23
        JP NC,main
        LD A,(IX+0)
        LD (MWin+0),A
        LD A,(IX+1)
        LD (MWin+1),A
        JP Main

m_mnu   DB 0,0,32,2

MWin    DEFB 0,0,1,1,7*8,7,%00001001
; Bit 0 - окно с заголовком/без
; Bit 1 - "перерисовать" окно да/нет
; Bit 2 - тискать окно по экрану да/нет
; Bit 3 - выход из окна-ткнуть только лишь за пределами (CANCEL)
        DEFB 0
        DEFW Nil          ; объект-текст
        DEFW Nil          ; объект-подсветка (выборка)
        DEFW HMain        ; горячие клавиши
manbut  DEFW BUTTONS      ; объект-кнопки (выборка)
        DEFW Nil          ; объект-флажки
        DEFW Nil          ; объект-радио кнопки
        DEFW Nil          ; ползунок
        DEFW Nil          ; вызов при создании
        DEFW Nil          ; вызов на "прерываниях"
        DEFW #FFFF

HMain   DB "C",0
         DW 7
        DB "K",0
         DW MainCol
        DB SS,"L"
         DW dldscr
        DB SS,"K"
         DW dsvscr
        DB SS,"J"
         DW ELdBLC
        DB 13,0
         DW 18
        DB "U",0
         DW 17
        DB "J",0
         DW 19
        DB "L",0
         DW SCRAM
        DB SS,"T"
         DW SAVERAM
        DB SS,"R"
         DW LOADRAM
        DB "W",0
         DW EWin8
        DB "S",0
         DW EWin1
        DB "M",0
         DW 8
        DB SS,"X"
         DW CLSSCRSCR
        DB SS,"C"
         DW CLSATR
        DB "G",0
         DW GRIDON
        DB "H",0
         DW GRIDOFF
        DB SS,"H"
         DW GRIDOFF_
        DB "N",0
         DW ESolidE
        DB "F",0
         DW ETFillE
        DB SS,"W"
         DW ETWashE
        DB CS,"W"
         DW ETWashe
        DB "D",0
         DW MSCR
        DB "B",0
         DW ETekBox
        DB "E",0
         DW EChBut
        DB CS,"3"
         DW LDN
        DB CS,"4"
         DW LUP
        DB "Y",0
         DW SUNDO
        DB CS,"J"
         DW JBrush
        DB CS,"I"
         DW ECOORD
        DB "X",0
         DW ETekPgn
        DB "Z",0
         DW EExe256
        DB "Z",SS
         DW ELPgn
        DB SS,"D"
         DW Driv
        DB CS,"L"
         DW dldimg_
        DB CS,"K"
         DW dsvimg_
        DB "V",0
         DW M_MNU
        DB #FF

dldimg_ CALL dldimg
        JP main_
dsvimg_ CALL dsvimg
        JP main_
Driv    CALL Driver
        JP main_
ELPgn   CALL LPgn
        JP main_
EEXE256 CALL otstoj
        JR otstoj2
EExe256 LD IX,WSPgns
        CALL POPUP
otstoj2 CALL exe256
        JP main_
ETekPgn CALL tekpgn
        JP main_
MainCol CALL Col
        JP main_

; +16
; x, y, номер опции, кол-во, текущий
; адрес обработчика OnClick
RADIO   DEFB 8,4,1,3,0
        DEFW Nil
        DEFB #FF

; +14
; x, y, номер опции, состояние (#00/#FF)
SWITCH  DEFB 8,3,0,0
        DEFW Nil
        DEFB #FF

; x, y, длина, высота, флаг
; bit 0 - подсвечивать (да/нет)
; bit 1 - клавиша "заморожена"

SELECT  DEFB 1,3,8,1,%10
        DEFB 1,4,8,1,%10
        DEFB 1,5,8,1,%10
        DEFB 1,6,8,1,%10
        DEFB 1,7,8,1,%11
        DEFB 1,8,8,1,%11
        DEFB #FF

; x, y, длина, высота, цвет, флаг
; адрес спрайта кнопки
; флаги:
; Bit 0 - клавиша нажимается (yes/no)
; Bit 1 - клавиша "заморожена"

BUTTONS DEFB 0,0,2,2,7,%11
        DEFW DISKS
        DEFB 2,0,2,1,7,%11
        DEFW PGN1S
        DEFB 2,1,2,1,7,%11
        DEFW PGN2S
        DEFB 4,0,2,1,7,%11
        DEFW CHRS
        DEFB 4,1,2,1,7,%11
        DEFW CHRS+16
        DEFB 6,0,2,2,7,%11
        DEFW WIN8S
        DEFB 8,0,2,2,7,%11
        DEFW WIN1S
        DEFB 10,0,2,2,7,%11
        DEFW RGBS
        DEFB 12,0,2,2,7,%11
        DEFW MGFS
        DEFB 14,0,2,2,7,%11
        DEFW CNKS
        DEFB 16,0,2,2,7,%11
        DEFW NALS
        DEFB 18,0,2,2,7,%11
        DEFW EK2S
        DEFB 20,0,2,2,7,%11
        DEFW FILLS
        DEFB 22,0,2,2,7,%11
        DEFW PRIMS
        DEFB 24,0,2,2,7,%11
        DEFW BRUSHS
        DEFB 26,0,2,2,7,%11
        DEFW BOXS
        DEFB 28,0,2,2,7,%11
        DEFW OTHERS
        DEFB 30,0,2,2,7,%11
        DEFW UNDOS
        DEFB #FF
;---------------------------------------------------------------
        INCLUDE "Screens",#43
        INCLUDE "Add",#42
        INCLUDE "Utils1",#46
        INCLUDE "RGB1",#44
        INCLUDE "Primit1",#47
        INCLUDE "SPEdit",#49
        INCLUDE "FRamka",#4C
        INCLUDE "ScrAdd1",#8C
        INCLUDE "DEBLC",#8D
        INCLUDE "Pgns2",#C2
        INCLUDE "DEHRUST@",#C4
;-------------------------
;----- разные рабочие процедуры для встроенных плагинов
;-------------------------
MTFill  CALL SAVEOBJ
        LD A,2
        CALL LPGN1
        LD IX,WTFill
        CALL Win_
        CALL CLOSE_
        CP #FF
        JP Z,mtfill
        LD (TEXTNUMB),A
        LD (MTEX),A
        CALL GETTEX
        LD DE,TBUFF
        PUSH DE
        LD BC,32
        LDIR 
        LD A,7
        CALL LPGN1
        POP HL
        LD DE,TMBuff
        PUSH DE
        LD BC,32
        LDIR 
        POP DE
        LD (mchspr),DE
        JP bmgn

mtfill  LD A,7
        CALL LPGN1
        XOR A
        JP mchange
;----------------------
MWash   CALL SAVEOBJ
        LD A,2
        CALL LPGN1
        LD IX,WTFill
        CALL Win_
        CALL CLOSE_
        CP #FF
        JP Z,mtfill
        LD (TEXTNUMB),A
        CALL GETTEX
        CALL LOADSCR
        CALL LD_UNDO
        LD HL,#C000
        LD DE,#DB00
        LD BC,#C000
        CALL wss
        CALL CLS
        JR mtfill
;--------------
MTFill_ PUSH BC
        CALL SAVEOBJ
        LD A,2
        CALL LPGN1
        CALL SV_UNDO
        CALL LOADSCR
        POP HL
        CALL RCOD
        LD (CONSTS),HL
        LD A,(MTEX)
        LD (TEXTNUMB),A
        CALL TXTRFF
        LD A,7
        CALL LPGN1
        CALL SAVESCR
        JP Bmgn

MTEX    DB 0
;-------------------------
;--- сплошная заливка для лупы
;-------------------------
MSolid  PUSH BC
        CALL SAVEOBJ
        LD A,2
        CALL LPGN1
        CALL SV_UNDO
        CALL LOADSCR
        POP HL
        CALL RCOD
        LD (CONSTS),HL
        CALL SOLIDFF
        LD A,7
        CALL LPGN1
        CALL SAVESCR
        JP Bmgn

;-------------------- процентовка -------------------
PERCINI LD IX,WPERC
        LD A,Window
        CALL GUI
        CALL TABLO
        RET 

WPERC   DB 2,8,27,5,7*8,7,%1111,0
        DW TPERC
        DW #FFFF
TPERC   DB "Процесс_",13
        DB 0

ADRPRCN EQU #4963
;-------
PERC5
;ПРОЦЕНТОВКА.
;A - INPUT

HEIPRCN EQU 2
LENPRCN EQU 25

PRCN    LD HL,ADRPRCN+#0200
PRCN1B  LD E,A
        SRL A
        SRL A
        ADD A,.ADRPRCN+1;МЛАДШИЙ БАЙТ АДРЕСА
        SUB L
        JR Z,PRCN1C
        RET C
        LD C,A
        LD A,E
        AND 3
        JR NZ,PRCN2A
        DEC C
        RET Z
PRCN2A  LD A,%10101010
PRCN1D  LD B,HEIPRCN
        LD H,ADRPRCN/256+2
        LD (HL),A
        INC H
        XOR #FF
        DJNZ $-4
        INC L
        DEC C
        JR NZ,PRCN1D
PRCN1C  DEC L
        LD A,E
        AND 3
        RET Z
        ADD A,A
        LD D,0
        LD B,A
        SCF 
        RR D
        DJNZ $-3
PRCN1E  LD H,ADRPRCN/256+2
        LD B,HEIPRCN
        LD A,(HL)
        AND D
        LD (HL),A
        INC H
        DJNZ $-4
        RET 

TABLO   LD HL,ADRPRCN
        LD C,HEIPRCN+4
        SCF 
TABLO1  LD B,LENPRCN
        PUSH HL
TABLO2  SBC A,A
        LD (HL),A
        INC L
        DJNZ TABLO2
        SET 7,(HL)
        POP HL
        DEC L
        SET 0,(HL)
        INC L
        INC H
        LD A,C
        CP 3
        DEC C
        JR NZ,TABLO1
        RET 

RESCALE8
        CALL SAVEOBJ
        LD HL,WinCol
        LD DE,WinCol_
        LD BC,4
        LDIR 
        LD A,5
        CALL LPGN1
        CALL INSTL
        CALL LOADSCR
        LD IX,WinCol_
        CALL BoxCol
        CALL COMA
        LD IX,WDEST
        CALL R8Sel
        CALL COMA
        JR C,RSC8end
        CALL LOADSCR
        LD IX,WinCol_
        CALL INIresc
        CALL Rescale
        CALL SAVESCR
RSC8end LD A,4
        CALL LPGN1
        JP BWindow

COMA    LD B,2
maks    LD A,(IX)
        ADD A,A
        ADD A,A
        ADD A,A
        LD (IX),A
        INC IX
        DJNZ maks
        LD B,2
maks_   LD A,(IX)
        ADD A,A
        ADD A,A
        ADD A,A
        DEC A
        LD (IX),A
        INC IX
        DJNZ maks_
        RET 

WinCol_ DS 4
WDEST   DB 0,0,200,120,0,0

; - типа в 128 памяти не хватило:)
TFill   DB "Заливка",13
        DB "Сплошная",13
        DB "Текстурой_",13
        DB "Редактор_",13
        DB "Размывка",13
        DB "Размывка+",13
        DB "Открыть_",13
        DB "Сохранить_",13
        DB "Дисковод",0
cnkid   DB "CHANK"

TBUFF   DS 32
BGE_SPR INCBIN "bge_spr"
        ECODE                   
LEN=ECODE-#6000

        DISPLAY "(c) Sinn^dtr^fdm^2oo1"
        DISPLAY "Длина BGE 3.o5     :",LEN
        DISPLAY "Свободно для BGE   :",PgnAdr-$

;---------------------------------------------------------------

DISKS   EQU BGE_SPR
PGN1S   EQU DISKS+32
PGN2S   EQU PGN1S+16
CHRS    EQU PGN2S+16
WIN8S   EQU CHRS+32
WIN1S   EQU WIN8S+32
RGBS    EQU WIN1S+32
MGFS    EQU RGBS+32
CNKS    EQU MGFS+32
NALS    EQU CNKS+32
EK2S    EQU NALS+32
FILLS   EQU EK2S+32
PRIMS   EQU FILLS+32
BOXS    EQU PRIMS+32
OTHERS  EQU BOXS+32
UNDOS   EQU OTHERS+32
EXITS   EQU UNDOS+32
OFFS    EQU EXITS+32
ONS     EQU OFFS+32
DTRS    EQU ONS+32
PLOTS   EQU DTRS+32
LINES   EQU PLOTS+32
LINES2  EQU LINES+32
TRS     EQU LINES2+32
SQS     EQU TRS+32
CIRCS   EQU SQS+32
ELPS    EQU CIRCS+32
RAYS    EQU ELPS+32
XORS    EQU RAYS+32
ANDS    EQU XORS+32
ORS     EQU ANDS+32
NOPS    EQU ORS+32
ERASES  EQU NOPS+32
SETKAS  EQU ERASES+32
RAMKAS  EQU SETKAS+32
FILLS2  EQU RAMKAS+32
AMASKS  EQU FILLS2+32
NORGBS  EQU AMASKS+32
WASHS1  EQU NORGBS+32
BRUSHS  EQU WASHS1+32

        ORG #C000,PgnPage1
        INCLUDE "MISC1",#45
        INCLUDE "Fill1",#4A
        INCLUDE "Chnk1",#4B
        INCLUDE "WinCol1",#88
        INCLUDE "WinPix3",#4D
        INCLUDE "Magnify2",#8B
        DISPLAY "Pgns1 [all] [free] : ",$-#C000,", ",#FFFF-$

        ORG #DB00,PgnPage2
        INCLUDE "Chr1",#4E
        INCLUDE "Fnt1",#4F
        INCLUDE "Brush1",#84
        INCLUDE "CoordXY",#85
        INCLUDE "SpHack",#C0
        INCLUDE "Box1",#48

        DISPLAY "Pgns2 [all] [free] : ",$-#DB00,", ",#FFFF-$

        ORG RUN

