void calc(void) { int i, x, y; for(i = 0; i < SzX; i++) /* assumes SzX = SzY */ for(x = i; x < SzX-i; x++) for(y = i; y < SzY-i; y++) if(grid[x][y] != 999) grid[x][y] = score1(x, y); }
// probmod<-function(M,h,Sin,Sout,STin,STout,Freq.in,Freq.out,toll) LISTA *probmod1(LISTA *ris, MATRICEi *M, VETTOREi *h, VETTOREi *Sin, VETTOREi *Sout, VETTOREd *STin, VETTOREd *STout, VETTOREd *Freq_in, VETTOREd *Freq_out, VETTOREd *toll) { int nh, ng, i, j, L, ind2, ind; int n_out, n_in; GString *lab = NULL; double mn, Pm, somma; MATRICEi *checkOUT = NULL, *checkIN = NULL, *memory = NULL; VETTOREi *M_in = NULL, *M_out = NULL; VETTOREi *tmp_i1 = NULL, *tmp_i2 = NULL, *indok = NULL, *indInf = NULL, *I = NULL, *ord_ind = NULL, *aus = NULL, *rs = NULL, *ind1 = NULL, *scalare_i = NULL, *I_add = NULL; VETTOREd *S_in = NULL, *S_out = NULL, *tmp_d1 = NULL; MATRICEd *Sc = NULL; enum TIPO tipi[5]; _Intestazione("\n*** probmod1 ***\n"); CREAv_i(scalare_i, 1); // nh<-length(h) nh = LENGTHv_i(h); // ng<-dim(M)[1] ng = LENGTHm1_i(M); // Sc<-checkIN<-checkOUT<-matrix(0,ng,nh) CREAm_i(checkOUT, ng, nh); InitMatr_i(checkOUT, 0); CREAm_i(checkIN, ng, nh); InitMatr_i(checkIN, 0); // Sc potrebbe gia` essere stato allocato! if (ris != NULL) { CtrlLlst(ris, 3); Sc = ACCEDIlst(ris, 3, md); } CREAm_d(Sc, ng, nh); InitMatr_d(Sc, 0.0); // M.in<-apply(M,1,sum) M_in = somma_righe_i(M_in, M); // M.out<-apply(M,2,sum) M_out = somma_colonne_i(M_out, M); // memory<-matrix(NA,ng,3) CREAm_i(memory, ng, 3); InitMatr_i(memory, NA_INTEGER); // Pm<-1 Pm = 1.0; // lab<-"NO" CREAstr(lab, "NO"); // i<-0 i = 0; // while (i<ng) while (i < ng) { // {i<-i+1 i++; // n.out<-M.out[i] n_out = ACCEDIv_i(M_out, i); // n.in<-M.in[i] n_in = ACCEDIv_i(M_in, i); // ind1<-intersect(which(memory[,1]==n.in),which(memory[,2]==n.out)) tmp_i1 = which_m_colindxeq_i(tmp_i1, memory, 1, n_in); tmp_i2 = which_m_colindxeq_i(tmp_i2, memory, 2, n_out); ind1 = interseca_i(ind1, tmp_i1, tmp_i2); // if (length(ind1)>0) { if (LENGTHv_i(ind1) > 0) { //ind<-memory[ind1,3] ind = copia_m_colindx_i(memory, ind1, 3); // Sc[i,]<-Sc[ind] Sc = aggiungi_ms_riga_d(Sc, i, ACCEDImv_d(Sc, ind)); // checkIN[i,]<-checkIN[ind,] checkIN = aggiungi_riga_i(checkIN, i, checkIN, ind); // checkOUT[i,]<-checkOUT[ind,] checkOUT = aggiungi_riga_i(checkOUT, i, checkOUT, ind); } // else else { // {if (n.out!=0) if (n_out != 0) { // {S.out<-Score(S=Sout[h],ST=STout,Freq=Freq.out,n=n.out,toll=toll) tmp_i1 = copia_v_indx_i(tmp_i1, Sout, h); S_out = score1(S_out, tmp_i1, STout, Freq_out, n_out, toll); // indok<-which(S.out!=-Inf) indok = which_v_indxne_d(indok, S_out, R_NegInf); // checkOUT[i,indok]<-1 checkOUT = aggiungi_ms_rigaindx_i(checkOUT, i, indok, 1); // if (length(indok)<1) {Pm<-NA; i<-ng} if (LENGTHv_i(indok) < 1) { Pm = NA_REAL; i = ng; } // indInf <- setdiff(seq(1, nh, 1), indok) tmp_i1 = seq_i(tmp_i1, 1, nh, 1); indInf = setdiff_i(indInf, tmp_i1, indok); // S.out[indInf]<-min(c(0,S.out[indok]))-1 tmp_d1 = copia_v_indx_d(tmp_d1, S_out, indok); if (LENGTHv_d(tmp_d1) == 0) mn =-1.0; else mn = min_s_d(0.0, min_v_d(tmp_d1)) - 1; assegna1_vs_indx_d(S_out, indInf, mn); // } } // else { else { // S.out<-rep(0,nh) CREAv_d(S_out, nh); InitVett_d(S_out, 0.0); // checkOUT[i,]<-rep(1,nh) CREAv_i(tmp_i1, nh); InitVett_i(tmp_i1, 1); checkOUT = aggiungi_mv_riga_i(checkOUT, i, tmp_i1); } // if (n.in!=0) if (n_in != 0) { // {S.in<-Score(S=Sin[h],ST=STin,Freq=Freq.in,n=n.in,toll=toll) tmp_i1 = assegna_v_indxNA_i(tmp_i1, Sin, h); S_in = score1(S_in, tmp_i1, STin, Freq_in, n_in, toll); // indok<-which(S.in!=-Inf) indok = which_v_indxne_d(indok, S_in, R_NegInf); // checkIN[i,indok]<-1 checkIN = aggiungi_ms_rigaindx_i(checkIN, i, indok, 1); // if (length(indok)<1) {Pm<-NA; i<-ng; lab<-"in"} if (LENGTHv_i(indok) < 1) { Pm = NA_REAL; i = ng; lab = g_string_assign(lab, "in"); } // indInf <- setdiff(seq(1, nh, 1), indok) tmp_i1 = seq_i(tmp_i1, 1, nh, 1); indInf = setdiff_i(indInf, tmp_i1, indok); // S.in[indInf]<-min(c(0,S.in[indok]))-1 tmp_d1 = copia_v_indx_d(tmp_d1, S_in, indok); if (LENGTHv_d(tmp_d1) == 0) mn =-1.0; else mn = min_s_d(0.0, min_v_d(tmp_d1)) - 1; assegna1_vs_indx_d(S_in, indInf, mn); // } } // else {S.in<-rep(0,nh); checkIN[i,]<-1} else { CREAv_d(S_in, nh); InitVett_d(S_in, 0.0); checkIN = aggiungi_ms_riga_i(checkIN, i, 1); } // Sc[i,]<-S.out+S.in tmp_d1 = somma_vv_d(tmp_d1, S_out, S_in); Sc = aggiungi_mv_riga_d(Sc, i, tmp_d1); // } } // }#end while (i<ng) }//end while (i<ng); // if (!is.na(Pm)) if (Pm != NA_REAL) { // {Pm<-sum(Sc)/ng somma = somma_m_d(Sc); Pm = somma / ng; // #check in //check in; // aus<-apply(checkIN,2,sum) aus = somma_colonne_i(aus, checkIN); // L<-length(which(aus!=0)) tmp_i1 = which_v_indxne_i(tmp_i1, aus, 0); L = LENGTHv_i(tmp_i1); // if (L<ng) {Pm<-NA; i<-ng; lab<-"in"} if (L < ng) { Pm = NA_REAL; i = ng; lab = g_string_assign(lab, "in"); } // else else { // {rs<-apply(checkIN,1,sum) rs = somma_righe_i(rs, checkIN); // ord.ind<-order(rs) ord_ind = ordine_i(ord_ind, rs, false); // I<-vector() CREAv_i(I, 0); // j<-0 j = 0; // while (j<ng) while (j < ng) { // {j<-j+1 j++; // ind<-ord.ind[j] ind2 = ACCEDIv_i(ord_ind, j); // I.add<-which(checkIN[ind,]!=0) I_add = which_m_rowindxne_i(I_add, checkIN, ind2, 0); // I<-union(I,I.add) I = unione1_i(I, I_add); // if (length(I)<j) {Pm<-NA; j<-ng} if (LENGTHv_i(I) < j) { Pm = NA_REAL; j = ng; } // } } // } } // #check out //check out; // checkOUT[which(checkOUT<0)]<-0 assegna1_m_indxlt_i(g_checkOUT, g_checkOUT, 0, 0); // checkOUT[which(checkOUT>0)]<-1 assegna1_m_indxgt_i(g_checkOUT, g_checkOUT, 0, 1); // aus<-apply(checkOUT,2,sum) aus = somma_colonne_i(aus, checkOUT); // L<-length(which(aus!=0)) tmp_i1 = which_v_indxne_i(tmp_i1, aus, 0); L = LENGTHv_i(tmp_i1); // if (L<ng) {Pm<-NA; i<-ng} if (L < ng) { Pm = NA_REAL; i = ng; } // else else { // {rs<-apply(checkOUT,1,sum) rs = somma_righe_i(rs, checkOUT); // ord.ind<-order(rs) ord_ind = ordine_i(ord_ind, rs, false); // I<-vector() CREAv_i(I, 0); // j<-0 j = 0; // while (j<ng) while (j < ng) { // {j<-j+1 j++; // ind<-ord.ind[j] ind = ACCEDIv_i(ord_ind, j); // I.add<-which(checkOUT[ind,]!=0) I_add = which_m_rowindxne_i(I_add, checkOUT, ind, 0); // I<-union(I,I.add) I = unione1_i(I, I_add); // if (length(I)<j) {Pm<-NA; j<-ng} if (LENGTHv_i(I) < j) { Pm = NA_REAL; j = ng; } // } } // } } // }# end if (!is.na(Pm)) }// end if (!is_na(Pm)) // return(list(Pm,M,Sc,lab)) tipi[0] = VETTd; tipi[1] = MATRi; tipi[2] = MATRd; tipi[3] = STRINGA; // creo già qui il 5° elemento tipi[4] = VETTi; // in realta` CreaLISTA potrebbe anche fare a meno di questo controllo: e` solo per inizializzare alcuni suoi elementi if (ris == NULL) { CreaLISTA(ris, tipi, 5); CtrlLlst(ris, 1); CREAv_d(ACCEDIlst(ris, 1, vd), 1); CtrlLlst(ris, 3); CREAstr(ACCEDIlst(ris, 3, str), ""); // non occorre creare il 5' elemento, perche´ verra` semplicemente assegnato (quindi usato come un puntatore) } CtrlLlst(ris, 1); ASSEGNAv_d(ACCEDIlst(ris, 1, vd), 1, Pm); CtrlSlst(ris, 2); ASSEGNAlst(ris, 2, mi, M); CtrlSlst(ris, 3); ASSEGNAlst(ris, 3, md, Sc); CtrlLlst(ris, 4); g_string_assign(ACCEDIlst(ris, 4, str), lab->str); // ora cancello cio' che non mi serve piu' CANCELLAv_d(S_out); CANCELLAv_d(S_in); // Sc viene passata in uscita, NON devo cancellarla! //CANCELLAm_d(Sc); CANCELLAm_i(checkIN); CANCELLAm_i(checkOUT); CANCELLAm_i(memory); CANCELLAv_i(M_in); CANCELLAv_i(M_out); CANCELLAv_i(tmp_i1); CANCELLAv_i(tmp_i2); CANCELLAv_i(indok); CANCELLAv_i(indInf); CANCELLAv_i(I); CANCELLAv_i(ord_ind); CANCELLAv_i(aus); CANCELLAv_i(rs); CANCELLAv_i(ind1); CANCELLAv_i(scalare_i); CANCELLAv_i(I_add); CANCELLAv_d(S_in); CANCELLAv_d(S_out); CANCELLAv_d(tmp_d1); StrBilanciam(); return ris; // } }
// MOD3<-function(Ng.out,Ng.in,STin,STout,max.con,Cg=0) MATRICEi *mod3(MATRICEi *ris, int Ng_out, int Ng_in, const VETTOREd *STin, const VETTOREd *STout, int max_con, double Cg) { int N, j, n_reg, L, Ls, ns; double S, tmpd; int aus; #ifdef MDEBUG GString *tmp = NULL; #endif _Intestazione("\n***mod3***\n"); #ifdef DET fprintf(fp_det, "input:\n"); fprintf(fp_det, "\tNg_out = %d\n", Ng_out); fprintf(fp_det, "\tNg_in = %d\n", Ng_in); fprintf(fp_det, "\tSTin = "); _StampaRawVett_d(STin); fprintf(fp_det, "\tSTout = "); _StampaRawVett_d(STout); fprintf(fp_det, "\tmax_con = %d\n", max_con); fprintf(fp_det, "\tCg = %.16g\n", Cg); #endif /* #ifdef FDEBUG fprintf(fp_fdbg, "Ng_out = %d\n", Ng_out); fprintf(fp_fdbg, "Ng_in = %d\n", Ng_in); _StampaVett_d(STin); _StampaVett_d(STout); fprintf(fp_fdbg, "max_con = %d\nCg = %.5e\n", max_con, Cg); fprintf(fp_fdbg, "\n"); fprintf(fp_fdbg, "*****************************************\n\n"); #endif*/ CREAv_d(g_scalare_d, 1); CREAv_i(g_scalare_i, 1); // ris<-matrix(0,Ng.out+Ng.in,Ng.out+Ng.in) CREAm_i(ris, Ng_out + Ng_in, Ng_out + Ng_in); InitMatr_i(ris, 0); // N<-Ng.in+Ng.out N = Ng_in + Ng_out; // S<-sum(STout[2:(Ng.in+1)],na.rm=TRUE) g_tmp1_d = segmento_v_d(g_tmp1_d, STout, 2, Ng_in + 1); S = somma_v_d(g_tmp1_d, true); // if (S>0) g_p<-STout[2:(Ng.in+1)]/S if (S > 0.0) { g_p = segmento_v_d(g_p, STout, 2, Ng_in + 1); dividi1_vs_d(g_p, S); } // else g_p<-rep(1/Ng.in,Ng.in) else { #ifdef MDEBUG if (Ng_in == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 53): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif CREAv_d(g_p, Ng_in); InitVett_d(g_p, (double) 1.0 / Ng_in); } // M.out<-sampleB(seq(1,Ng.in,1),Ng.out,prob=g_p,replace=TRUE) g_tmp1_i = seq_i(g_tmp1_i, 1, Ng_in, 1); g_M_out = sampleB_p(g_M_out, g_tmp1_i, Ng_out, 1, g_p); g_tmpSTin = copia_v_d(g_tmpSTin, STin, 1, LENGTHv_d(STin)); g_tmpSTout = copia_v_d(g_tmpSTout, STout, 1, LENGTHv_d(STout)); // while (sum(M.out)<(N-1)) # rm.na == FALSE while (somma_v_i(g_M_out, false) < N - 1) { // {if (S>0) if (S > 0.0) { // {STout<-(STout[1:(Ng.in+1)]/S)*Ng.in segmento1_v_d(g_tmpSTout, 1, Ng_in + 1); #ifdef MDEBUG if (Ng_in == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 74): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif dividi1_vs_d(g_tmpSTout, (double) S / Ng_in); // aus<-max(M.out)+1 aus = max_v_i(g_M_out) + 1; // Freq.out<-hist(M.out,breaks=seq(0,Ng.in+1,1),right=FALSE,plot=FALSE)$counts/N g_tmp2_i = seq_i(g_tmp2_i, 0, Ng_in + 1, 1); g_tmp1_i = hist1(g_tmp1_i, g_M_out, g_tmp2_i, 0, 1, 0); g_Freq_out = dividi_vs_i(g_Freq_out, g_tmp1_i, (double) N); // g_Sc<-Score(S=M.out,ST=STout,Freq=Freq.out,n=1,toll=rep(Inf,(Ng.out+1))) CREAv_d(g_tmp3_d, Ng_out + 1); InitVett_d(g_tmp3_d, R_PosInf); g_Sc = score1(g_Sc, g_M_out, g_tmpSTout, g_Freq_out, 1, g_tmp3_d); // g_indok<-which(g_Sc!=-Inf) g_indok = which_v_indxne_d(g_indok, g_Sc, R_NegInf); // g_indInf <- setdiff(seq(1, Ng.out, 1), g_indok) g_tmp3_i = seq_i(g_tmp3_i, 1, Ng_out, 1); g_indInf = setdiff_i(g_indInf, g_tmp3_i, g_indok); // g_Sc[g_indInf]<-min(c(0,g_Sc[g_indok]))-1 g_tmp1_d = assegna_v_indx_d(g_tmp1_d, g_Sc, g_indok); tmpd = min_v_d(g_tmp1_d); if (tmpd < 0.0) tmpd = 0.0; assegna1_v_indx_d(g_Sc, g_indInf, tmpd - 1); // g_p<-g_Sc/sum(g_Sc) g_p = dividi_vs_d(g_p, g_Sc, somma_v_d(g_Sc, false)); // } } // else g_p<-rep(1/Ng.in,Ng.in) else { #ifdef MDEBUG if (Ng_in == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 111): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif CREAv_d(g_p, Ng_in); InitVett_d(g_p, (double) 1.0 / Ng_in); } // g_ind.M<-sampleB(seq(1,Ng.out,1),1,prob=g_p) g_tmp2_i = seq_i(g_tmp2_i, 1, Ng_out, 1); g_ind_M = sampleB_p(g_ind_M, g_tmp2_i, 1, 0, g_p); // M.out[g_ind.M]<-M.out[g_ind.M]+1 ASSEGNAv_i(g_M_out, ACCEDIv_i(g_ind_M, 1), ACCEDIv_i(g_M_out, ACCEDIv_i(g_ind_M, 1)) + 1); } // for (j in (1:Ng.out)) for (j = 1; j <= Ng_out; j++) { // {n.reg<-M.out[j] n_reg = ACCEDIv_i(g_M_out, j); // if (j==1) if (j == 1) { // {ris[1:n.reg,(j+Ng.in)]<-1 g_tmp1_i = seq_i(g_tmp1_i, 1, n_reg, 1); ASSEGNAv_i(g_scalare_i, 1, j + Ng_in); assegna1_m_vv_i(ris, g_tmp1_i, g_scalare_i, 1); // g_indS<-1:n.reg g_indS = seq_i(g_indS, 1, n_reg, 1); // g_indBS<-setdiff(1:Ng.in,g_indS) g_tmp2_i = seq_i(g_tmp2_i, 1, Ng_in, 1); g_indBS = setdiff_i(g_indBS, g_tmp2_i, g_indS); // } } // else else { // {L<-length(g_indBS) L = LENGTHv_i(g_indBS); // Ls<-min(L,n.reg-1) Ls = min_s_i(L, n_reg - 1); // ns<-n.reg-Ls ns = n_reg - Ls; // S<-sum(STin[1:(Ng.out+1)],na.rm=TRUE) g_tmp1_d = segmento_v_d(g_tmp1_d, g_tmpSTin, 1, Ng_out + 1); S = somma_v_d(g_tmp1_d, true); // g_Sin<-apply(ris,1,sum) g_Sin = somma_righe_i(g_Sin, ris); // if (S>0) if (S > 0.0) { // {STin<-(STin[1:(Ng.out+1)]/S)*Ng.out segmento1_v_d(g_tmpSTin, 1, Ng_out + 1); #ifdef MDEBUG if (Ng_out == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 163): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif dividi1_vs_d(g_tmpSTin, (double) S / Ng_out); // aus<-max(g_Sin)+1 aus = max_v_i(g_Sin) + 1; // Freq.in<-hist(g_Sin,breaks=seq(0,aus,1),right=FALSE,plot=FALSE)$counts/N g_tmp2_i = seq_i(g_tmp2_i, 0, aus, 1); g_tmp1_i = hist1(g_tmp1_i, g_Sin, g_tmp2_i, 0, 1, 0); g_Freq_in = dividi_vs_i(g_Freq_in, g_tmp1_i, (double) N); // g_Sc<-Score(S=g_Sin[g_indS],ST=STin,Freq=Freq.in,n=1,toll=rep(Inf,(Ng.out+1))) g_tmp2_i = assegna_v_indx_i(g_tmp2_i, g_Sin, g_indS); CREAv_d(g_tmp3_d, Ng_out + 1); InitVett_d(g_tmp3_d, R_PosInf); g_Sc = score1(g_Sc, g_tmp2_i, g_tmpSTin, g_Freq_in, 1, g_tmp3_d); // g_indok<-which(g_Sc!=-Inf) g_indok = which_v_indxne_d(g_indok, g_Sc, R_NegInf) ; // g_indInf <- setdiff(seq(1, length(g_indS), 1), g_indok) g_tmp1_i = seq_i(g_tmp1_i, 1, LENGTHv_i(g_indS), 1); g_indInf = setdiff_i(g_indInf, g_tmp1_i, g_indok); // g_Sc[g_indInf]<-min(c(0,g_Sc[g_indok]))-1 g_tmp1_d = assegna_v_indx_d(g_tmp1_d, g_Sc, g_indok); tmpd = min_v_d(g_tmp1_d); if (tmpd < 0.0) tmpd = 0.0; assegna1_v_indx_d(g_Sc, g_indInf, tmpd - 1); // g_p.sc<-g_Sc/sum(g_Sc) g_p_sc = dividi_vs_d(g_p_sc, g_Sc, somma_v_d(g_Sc, false)); // } } // else g_p.sc<-rep(1/length(g_indS),length(g_indS)) else { #ifdef MDEBUG if (LENGTHv_i(g_indS) == 0) { CREAstr(tmp, ""); g_string_printf(tmp, "ATTENZIONE (mod3.c, linea 201): divisione per zero!\n"); warning(tmp->str); fprintf(fp_fdbg, tmp->str); CANCELLAstr(tmp); } #endif CREAv_d(g_p_sc, LENGTHv_i(g_indS)); InitVett_d(g_p_sc, (double) 1.0 / LENGTHv_i(g_indS)); } // // g_ind<-which(g_Sin[g_indS]==max.con) g_ind = which_v_indxeq_i(g_ind, g_tmp1_i, max_con); // g_p.sc[g_ind]<-0 assegna1_vs_indx_d(g_p_sc, g_ind, 0.0); // g_p.sc<-g_p.sc/sum(g_p.sc) g_p = dividi_vs_d(g_p, g_p_sc, somma_v_d(g_p_sc, false)); // g_ind1<-sampleB(g_indS,ns,prob=g_p.sc) g_ind1 = sampleB_p(g_ind1, g_indS, ns, 0, g_p_sc); // if (Ls>0) if (Ls > 0) { // {g_ind<-g_indBS[1:Ls] g_ind = segmento_v_i(g_ind, g_indBS, 1, Ls); // g_indS<-c(g_indS,g_ind) g_indS = accoda1_vv_i(g_indS, g_ind); // g_indBS<-setdiff(1:Ng.in,g_indS) g_tmp1_i = seq_i(g_tmp1_i, 1, Ng_in, 1); g_indBS = setdiff_i(g_indBS, g_tmp1_i, g_indS); // g_ind1<-c(g_ind1,g_ind) g_ind1 = accoda1_vv_i(g_ind1, g_ind); // } } // m[g_ind1,(j+Ng.in)]<-1 ASSEGNAv_i(g_scalare_i, 1, j + Ng_in); assegna1_m_vv_i(ris, g_ind1, g_scalare_i, 1); // } } // } } // if ( (Cg>0)&((Ng.in+Ng.out)>2) ) ris<-triangola(M=ris,Cg=Cg,max.con) if (Cg > 0.0 && Ng_in + Ng_out > 2) ris = triangola(ris, Cg, max_con); // return(ris) //~ CANCELLAv_i(g_M_out); //~ CANCELLAv_i(g_indok); //~ CANCELLAv_i(g_tmp3_i); //~ CANCELLAv_d(g_tmp1_d); //~ CANCELLAv_d(g_Freq_out); //~ CANCELLAv_d(g_tmp3_d); //~ CANCELLAv_d(g_p); //~ CANCELLAv_d(g_Sc); //~ CANCELLAv_i(g_tmp2_i); //~ CANCELLAv_i(g_ind_M); //~ CANCELLAv_i(g_ind1); //~ CANCELLAv_i(g_indS); //~ CANCELLAv_i(g_indBS); //~ CANCELLAv_i(g_Sin); //~ CANCELLAv_i(g_ind); //~ CANCELLAv_i(g_indInf); //~ CANCELLAv_i(g_tmp1_i); //~ CANCELLAv_i(g_scalare_i); //~ CANCELLAv_d(g_scalare_d); //~ CANCELLAv_d(g_p_sc); #ifdef FDEBUG fprintf(fp_fdbg, "\n\n"); fprintf(fp_fdbg, "*****************************************\n"); fprintf(fp_fdbg, "*****************************************\n\n"); #endif StrBilanciam(); #ifdef DET fprintf(fp_det, "mod3 output:\n"); fprintf(fp_det, "\tm = "); _StampaRawMatr_i(ris); #endif return ris; // } }
MATRICEd *probmod2(MATRICEd *ris, const MATRICEi *M, const VETTOREi *h, const VETTOREi *Sin, const VETTOREi *Sout, const VETTOREd *STin, const VETTOREd *STout, const VETTOREd *Freq_in, const VETTOREd *Freq_out, const VETTOREd *toll, struct RisProbM *ris1) { int nh, ng, i, j, L, ind2, ind; int n_out, n_in; char lab[3]; double mn, Pm, somma; _Intestazione("\n***probmod***\n"); #ifdef DET fprintf(fp_det, "input:\n"); fprintf(fp_det, "\tM = "); _StampaRawMatr_i(M); fprintf(fp_det, "\th = "); _StampaRawVett_i(h); fprintf(fp_det, "\tSin = "); _StampaRawVett_i(Sin); fprintf(fp_det, "\tSout = "); _StampaRawVett_i(Sout); fprintf(fp_det, "\tSTin = "); _StampaRawVett_d(STin); fprintf(fp_det, "\tSTout = "); _StampaRawVett_d(STout); fprintf(fp_det, "\tFreq_in = "); _StampaRawVett_d(Freq_in); fprintf(fp_det, "\tFreq_out = "); _StampaRawVett_d(Freq_out); fprintf(fp_det, "\ttoll = "); _StampaRawVett_d(toll); #endif CREAv_i(g_scalare_i, 1); // nh<-length(h) nh = LENGTHv_i(h); // ng<-dim(M)[1] ng = LENGTHm1_i(M); // g_Sc<-g_checkIN<-g_checkOUT<-matrix(0,ng,nh) CREAm_i(g_checkOUT, ng, nh); InitMatr_i(g_checkOUT, 0); CREAm_i(g_checkIN, ng, nh); InitMatr_i(g_checkIN, 0); CREAm_d(ris, ng, nh); InitMatr_d(ris, 0.0); // M.in<-apply(M,1,sum) g_M_in = somma_righe_i(g_M_in, M); // M.out<-apply(M,2,sum) g_M_out = somma_colonne_i(g_M_out, M); // g_memory<-matrix(NA,ng,3) CREAm_i(g_memory, ng, 3); InitMatr_i(g_memory, NA_INTEGER); // Pm<-1 Pm = 1.0; // lab<-"NO" strncpy(lab, "NO", 3); // i<-0 i = 0; // while (i<ng) while (i < ng) { // {i<-i+1 i++; // n.out<-M.out[i] n_out = ACCEDIv_i(g_M_out, i); // n.in<-M.in[i] n_in = ACCEDIv_i(g_M_in, i); // g_ind1<-intersect(which(g_memory[,1]==n.in),which(g_memory[,2]==n.out)) g_tmp_i1 = which_m_colindxeq_i(g_tmp_i1, g_memory, 1, n_in); g_tmp_i2 = which_m_colindxeq_i(g_tmp_i2, g_memory, 2, n_out); g_ind1 = interseca_i(g_ind1, g_tmp_i1, g_tmp_i2); // if (length(g_ind1)>0) { if (LENGTHv_i(g_ind1) > 0) { //ind<-g_memory[g_ind1,3] ind = copia_m_colindx_i(g_memory, g_ind1, 3); // g_Sc[i,]<-g_Sc[ind] ris = aggiungi_ms_riga_d(ris, i, ACCEDImv_d(ris, ind)); // g_checkIN[i,]<-g_checkIN[ind,] g_checkIN = aggiungi_riga_i(g_checkIN, i, g_checkIN, ind); // g_checkOUT[i,]<-g_checkOUT[ind,] g_checkOUT = aggiungi_riga_i(g_checkOUT, i, g_checkOUT, ind); } // else else { // {if (n.out!=0) if (n_out != 0) { // {S.out<-Score(S=Sout[h],ST=STout,Freq=Freq.out,n=n.out,toll=toll) g_tmp_i1 = assegna_v_indxNA_i(g_tmp_i1, Sout, h); g_S_out = score1(g_S_out, g_tmp_i1, STout, Freq_out, n_out, toll); // indok<-which(S.out!=-Inf) g_tmp_i3 = which_v_indxne_d(g_tmp_i3, g_S_out, R_NegInf); // g_checkOUT[i,indok]<-1 g_checkOUT = aggiungi_ms_rigaindx_i(g_checkOUT, i, g_tmp_i3, 1); // if (length(indok)<1) {Pm<-NA; i<-ng} if (LENGTHv_i(g_tmp_i3) < 1) { Pm = NA_REAL; i = ng; } // g_indInf <- setdiff(seq(1, nh, 1), indok) g_tmp_i1 = seq_i(g_tmp_i1, 1, nh, 1); g_indInf = setdiff_i(g_indInf, g_tmp_i1, g_tmp_i3); // S.out[g_indInf]<-min(c(0,S.out[indok]))-1 g_tmp_d1 = copia_v_indx_d(g_tmp_d1, g_S_out, g_tmp_i3); if (LENGTHv_d(g_tmp_d1) == 0) mn =-1.0; else mn = min_s_d(0.0, min_v_d(g_tmp_d1)) - 1; assegna1_vs_indx_d(g_S_out, g_indInf, mn); // } } // else { else { // S.out<-rep(0,nh) CREAv_d(g_S_out, nh); InitVett_d(g_S_out, 0.0); // g_checkOUT[i,]<-rep(1,nh) CREAv_i(g_tmp_i1, nh); InitVett_i(g_tmp_i1, 1); g_checkOUT = aggiungi_mv_riga_i(g_checkOUT, i, g_tmp_i1); } // if (n.in!=0) if (n_in != 0) { // {S.in<-Score(S=Sin[h],ST=STin,Freq=Freq.in,n=n.in,toll=toll) g_tmp_i1 = assegna_v_indxNA_i(g_tmp_i1, Sin, h); g_S_in = score1(g_S_in, g_tmp_i1, STin, Freq_in, n_in, toll); // indok<-which(S.in!=-Inf) g_tmp_i3 = which_v_indxne_d(g_tmp_i3, g_S_in, R_NegInf); // g_checkIN[i,indok]<-1 g_checkIN = aggiungi_ms_rigaindx_i(g_checkIN, i, g_tmp_i3, 1); // if (length(indok)<1) {Pm<-NA; i<-ng; lab<-"in"} if (LENGTHv_i(g_tmp_i3) < 1) { Pm = NA_REAL; i = ng; strncpy(lab, "in", 3); } // g_indInf <- setdiff(seq(1, nh, 1), indok) g_tmp_i1 = seq_i(g_tmp_i1, 1, nh, 1); g_indInf = setdiff_i(g_indInf, g_tmp_i1, g_tmp_i3); // S.in[g_indInf]<-min(c(0,S.in[indok]))-1 g_tmp_d1 = copia_v_indx_d(g_tmp_d1, g_S_in, g_tmp_i3); if (LENGTHv_d(g_tmp_d1) == 0) mn =-1.0; else mn = min_s_d(0.0, min_v_d(g_tmp_d1)) - 1; assegna1_vs_indx_d(g_S_in, g_indInf, mn); // } } // else {S.in<-rep(0,nh); g_checkIN[i,]<-1} else { CREAv_d(g_S_in, nh); InitVett_d(g_S_in, 0.0); g_checkIN = aggiungi_ms_riga_i(g_checkIN, i, 1); } // g_Sc[i,]<-S.out+S.in g_tmp_d1 = somma_vv_d(g_tmp_d1, g_S_out, g_S_in); ris = aggiungi_mv_riga_d(ris, i, g_tmp_d1); // } } // }#end while (i<ng) }//end while (i<ng); // if (!is.na(Pm)) if (!ISNA(Pm)) { // {Pm<-sum(g_Sc)/ng somma = somma_m_d(ris); Pm = somma / ng; // #check in //check in; // aus<-apply(g_checkIN,2,sum) g_tmp_i3 = somma_colonne_i(g_tmp_i3, g_checkIN); // L<-length(which(aus!=0)) g_tmp_i1 = which_v_indxne_i(g_tmp_i1, g_tmp_i3, 0); L = LENGTHv_i(g_tmp_i1); // if (L<ng) {Pm<-NA; i<-ng; lab<-"in"} if (L < ng) { Pm = NA_REAL; i = ng; strncpy(lab,"in", 3); } // else else { // {g_rs<-apply(g_checkIN,1,sum) g_rs = somma_righe_i(g_rs, g_checkIN); // ord.ind<-order(g_rs) g_ord_ind = ordine_i(g_ord_ind, g_rs, false); // g_I<-vector() CREAv_i(g_I, 0); // j<-0 j = 0; // while (j<ng) while (j < ng) { // {j<-j+1 j++; // ind<-ord.ind[j] ind2 = ACCEDIv_i(g_ord_ind, j); // g_I.add<-which(g_checkIN[ind,]!=0) g_I_add = which_m_rowindxne_i(g_I_add, g_checkIN, ind2, 0); // g_I<-union(g_I,g_I.add) g_I = unione1_i(g_I, g_I_add); // if (length(g_I)<j) {Pm<-NA; j<-ng} if (LENGTHv_i(g_I) < j) { Pm = NA_REAL; j = ng; } // } } // } } // #check out //check out; // g_checkOUT[which(g_checkOUT<0)]<-0 assegna1_m_indxlt_i(g_checkOUT, g_checkOUT, 0, 0); // g_checkOUT[which(g_checkOUT>0)]<-1 assegna1_m_indxgt_i(g_checkOUT, g_checkOUT, 0, 1); // aus<-apply(g_checkOUT,2,sum) g_tmp_i3 = somma_colonne_i(g_tmp_i3, g_checkOUT); // L<-length(which(aus!=0)) g_tmp_i1 = which_v_indxne_i(g_tmp_i1, g_tmp_i3, 0); L = LENGTHv_i(g_tmp_i1); // if (L<ng) {Pm<-NA; i<-ng} if (L < ng) { Pm = NA_REAL; i = ng; } // else else { // {g_rs<-apply(g_checkOUT,1,sum) g_rs = somma_righe_i(g_rs, g_checkOUT); // ord.ind<-order(g_rs) g_ord_ind = ordine_i(g_ord_ind, g_rs, false); // g_I<-vector() CREAv_i(g_I, 0); // j<-0 j = 0; // while (j<ng) while (j < ng) { // {j<-j+1 j++; // ind<-ord.ind[j] ind = ACCEDIv_i(g_ord_ind, j); // g_I.add<-which(g_checkOUT[ind,]!=0) g_I_add = which_m_rowindxne_i(g_I_add, g_checkOUT, ind, 0); // g_I<-union(g_I,g_I.add) g_I = unione1_i(g_I, g_I_add); // if (length(g_I)<j) {Pm<-NA; j<-ng} if (LENGTHv_i(g_I) < j) { Pm = NA_REAL; j = ng; } // } } // } } // }# end if (!is.na(Pm)) }// end if (!is_na(Pm)) // return(list(Pm,M,g_Sc,lab)) ris1->score = Pm; // ho assegnato prima il viceversa, quindi non dovrebbe servire //~ g_score_matr = g_Sc; strncpy(ris1->label, lab, 3); // ora cancello cio' che non mi serve piu` //~ CANCELLAv_d(g_S_out); //~ CANCELLAv_d(g_S_in); //~ //CANCELLAm_d(g_Sc); //~ CANCELLAm_i(g_checkIN); //~ CANCELLAm_i(g_checkOUT); //~ CANCELLAm_i(g_memory); //~ CANCELLAv_i(g_M_in); //~ CANCELLAv_i(g_M_out); //~ CANCELLAv_i(g_tmp_i1); //~ CANCELLAv_i(g_tmp_i2); //~ CANCELLAv_i(g_tmp_i3); //~ CANCELLAv_i(g_indInf); //~ CANCELLAv_i(g_I); //~ CANCELLAv_i(g_ord_ind); //~ CANCELLAv_i(g_rs); //~ CANCELLAv_i(g_ind1); //~ CANCELLAv_i(g_scalare_i); //~ CANCELLAv_i(g_I_add); //~ CANCELLAv_d(g_S_in); //~ CANCELLAv_d(g_S_out); //~ CANCELLAv_d(g_tmp_d1); StrBilanciam(); #ifdef DET fprintf(fp_det, "probmod output:\n"); fprintf(fp_det, "\tlist(Pm,M,Sc,lab) = %.16g\n", ris1->score); _StampaRawMatr_i(M); _StampaRawMatr_d(ris); fprintf(fp_det, "%s\n", ris1->label); #endif return ris; // } }
int main(){ ////////////////////GAME PROPERTIES/////////////////////////////////// //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "Tungu"); pong.setMouseCursorVisible(false); pong.setFramerateLimit(60); //music sf::Music bgm; bgm.openFromFile("multimedia/audio/background.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("multimedia/audio/bounce.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("multimedia/audio/point.wav"); sf::Sound point; point.setBuffer(buffer2); //ncp properties sf::RectangleShape ncp(sf::Vector2f(5, RENDERHEIGHT / 1.6)); ncp.setFillColor(sf::Color(50, 50, 50)); ncp.setPosition(RENDERWIDTH / 2, RENDERHEIGHT / 2 - (RENDERHEIGHT / 1.6) / 2 ); //player 1 properties int p1Len = 100; sf::RectangleShape player1(sf::Vector2f(15, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 100; sf::RectangleShape player2(sf::Vector2f(15, p2Len)); player2.setFillColor(sf::Color(0, 255, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(10, 25); ball.setFillColor(sf::Color(255, 255, 255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 15; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; //score-timer text sf::Font font; font.loadFromFile("fonts/LiberationSerif-Regular.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); sf::Text timer1("", font, 50); timer1.setPosition(0 , 5 * RENDERHEIGHT / 6); sf::Text timer2("", font, 50); timer2.setPosition(RENDERWIDTH / 2 - timer2.getLocalBounds().width, 5 * RENDERHEIGHT / 6); int time1 = 0; int time2 = 0; //gameover sf::Text gameover("GAME OVER", font, 120); gameover.setColor(sf::Color::Red); gameover.setPosition(0, RENDERHEIGHT / 3); ///////////////CAMERA FUNTIONS////////////////////// CvCapture* capture =0; capture = cvCaptureFromCAM(0); if(!capture){ printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvZero(imgTracking); //covert the image, 'imgTracking' to black //cvNamedWindow("Player1"); ///cvNamedWindow("Player2"); //iterate through each frames of the video while(player1Score + player2Score != 7){ frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel /////////////////// Player 1 //////////////////// IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh = GetThresholdedImage(imgHSV, 85, 143, 40, 116, 256, 159); //guante cyan cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player100 = trackObject(imgThresh, 255, 0, 0, 1); /////////////////// Player 2 //////////////////// IplImage* imgHSV2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV2, CV_BGR2HSV);//Change the color format from BGR to HSV IplImage* imgThresh2 = GetThresholdedImage(imgHSV2, 26, 145, 31, 73, 256, 111); //guante verde cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player200 = trackObject(imgThresh2, 0, 255, 0, 2); // Add the tracking image and the frame cvAdd(frame, imgTracking, frame); //cvShowImage("Player1", imgThresh); //cvShowImage("Player2", imgThresh2); cvMoveWindow("Video", 1800, 0); cvResizeWindow("Video", 100, 240); cvShowImage("Video", frame); //Clean up used images cvReleaseImage(&imgHSV); cvReleaseImage(&imgHSV2); cvReleaseImage(&imgThresh); cvReleaseImage(&imgThresh2); cvReleaseImage(&frame); //Wait 10mS int c = cvWaitKey(10); //If 'ESC' is pressed, break the loop if((char)c==27 ) break; /////////////////////////////Scores/////////////////////////////////// //score for player one winning if(player1Score == player2Score + 1) { timer1.setString(convertInt(time1 += 2)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 2) { timer1.setString(convertInt(time1 += 4)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 3) { timer1.setString(convertInt(time1 += 8)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 4) { timer1.setString(convertInt(time1 += 16)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 5) { timer1.setString(convertInt(time1 += 32)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 6) { timer1.setString(convertInt(time1 += 64)); timer2.setString(convertInt(time2 += 1)); } //score on equals if(player1Score == player2Score) { timer1.setString(convertInt(time1 += 1)); timer2.setString(convertInt(time2 += 1)); } //score for player two winning if(player2Score == player1Score + 1) { timer2.setString(convertInt(time2 += 2)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 2) { timer2.setString(convertInt(time2 += 4)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 3) { timer2.setString(convertInt(time2 += 8)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 4) { timer2.setString(convertInt(time2 += 16)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 5) { timer2.setString(convertInt(time2 += 32)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 6) { timer2.setString(convertInt(time2 += 64)); timer1.setString(convertInt(time1 += 1)); } //////////////////////////////Game//////////////////////////////////// //player 1 movement muy lento if(player100==6) player1.move(0, -(vel-3)); else if(player100==-6) player1.move(0, vel-3); //player 1 movement LENTO if(player100==1) player1.move(0, -vel); else if(player100==-1) player1.move(0, vel); //player 1 movement MEDIO if(player100==2) player1.move(0, -(vel+10)); else if(player100==-2) player1.move(0, vel+10); //player 1 movement RAPIDO if(player100==3) player1.move(0, -(vel+20)); else if(player100==-3) player1.move(0, vel+20); //player 1 movement muy rapido if(player100==4) player1.move(0, -(vel+25)); else if(player100==-4) player1.move(0, vel+25); //player 1 movement Extreme if(player100==5) player1.move(0, -(vel+30)); else if(player100==-5) player1.move(0, vel+30); //MOVIMIENTO GOLPE PLAYER1 if(player100==0) player1.move(0,0); if (player1.getPosition().x <= 10){ if(player100==22) player1.move(10, 0); } //player 2 movement muy LENTO if(player200==6) player2.move(0, -(vel-3)); else if(player200==-6) player2.move(0, vel-3); //player 2 movement LENTO if(player200==1) player2.move(0, -vel); else if(player200==-1) player2.move(0, vel); //player 2 movement MEDIO if(player200==2) player2.move(0, -(vel+10)); else if(player200==-2) player2.move(0, vel+10); //player 2 movement RAPIDO if(player200==3) player2.move(0, -(vel+20)); else if(player200==-3) player2.move(0, vel+20); //player 2 movement muy rapido if(player200==4) player2.move(0, -(vel+25)); else if(player200==-4) player2.move(0, vel+25); //player 2 movement Extreme if(player200==5) player2.move(0, -(vel+30)); else if(player200==-5) player2.move(0, vel+30); if(player200==0) player2.move(0,0); //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(player200==-22) player2.move(-10, 0); } //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter){ ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x){ if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y){ if (player1.getPosition().x > 14){ ballVelX = (ballVelX - 5) * -1; ball.setFillColor(sf::Color(255,0,0)); } else if (player1.getPosition().x <= 14){ ballVelX = (ballVelX - 3) * -1; ball.setFillColor(sf::Color(0,0,255)); bounce.play(); } } else{ ball.setFillColor(sf::Color(255,255,255)); point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.2; ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 40) p2Len -= 10; player2.setSize(sf::Vector2f(15, p2Len)); if (p1Len < 100) p1Len += 10; player1.setSize(sf::Vector2f(15, p1Len)); } } //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x){ if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y){ if (player2.getPosition().x < (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 5) * -1; ball.setFillColor(sf::Color(255,0,0)); } else if (player2.getPosition().x >= (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 3) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); } } else{ ball.setFillColor(sf::Color(255,255,255)); point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 40) p1Len -= 10; player1.setSize(sf::Vector2f(15, p1Len)); if (p2Len < 100) p2Len += 10; player2.setSize(sf::Vector2f(15, p2Len)); } } //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(timer1); pong.draw(timer2); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.draw(ncp); pong.display(); while(player1Score + player2Score == 7){ if(player1Score > player2Score) timer1.setString(convertInt(time1 += 500)); if(player1Score < player2Score) timer2.setString(convertInt(time2 += 500)); pong.clear(sf::Color::Black); pong.draw(score1); pong.draw(timer1); pong.draw(timer2); pong.draw(score2); pong.draw(gameover); pong.display(); counter(3); break; } /////////////////////Finish Game///////////////////////////////// } std::stringstream ss; ss.str (timer1.getString()); std::string scorePlayer1 = ss.str(); ss.str (timer2.getString()); std::string scorePlayer2 = ss.str(); std::cout << "Final Score:" << '\n'; std::cout << "Player1: " + scorePlayer1 << '\n'; std::cout << "Player2: " + scorePlayer2 << '\n'; std::ofstream myfile ("highscores.txt", std::ofstream::in | std::ofstream::out | std::ofstream::app); if (myfile.is_open()) { myfile << scorePlayer1 << std::endl; myfile << scorePlayer2 << std::endl; myfile.close(); } else std::cout << "Unable to open file"; cvDestroyAllWindows() ; cvReleaseImage(&imgTracking); cvReleaseCapture(&capture); return 0; }
int main() { //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName", sf::Style::Fullscreen); pong.setMouseCursorVisible(false); pong.setFramerateLimit(60); //music sf::Music bgm; bgm.openFromFile("multimedia/audio/musica.wav"); bgm.setPitch(1); bgm.setLoop(true); bgm.play(); //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("multimedia/audio/bounce.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("multimedia/audio/point.wav"); sf::Sound point; point.setBuffer(buffer2); //ncp properties sf::RectangleShape ncp(sf::Vector2f(5, RENDERHEIGHT / 1.6)); ncp.setFillColor(sf::Color(50, 50, 50)); ncp.setPosition(RENDERWIDTH / 2, RENDERHEIGHT / 2 - (RENDERHEIGHT / 1.6) / 2 ); //player 1 properties int p1Len = 100; sf::RectangleShape player1(sf::Vector2f(10, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 100; sf::RectangleShape player2(sf::Vector2f(10, p2Len)); player2.setFillColor(sf::Color(255, 0, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(10, 25); ball.setFillColor(sf::Color(255, 255, 255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 2; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; //score-timer text sf::Font font; font.loadFromFile("fonts/LiberationSerif-Regular.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); sf::Text timer1("", font, 80); timer1.setPosition(0 , 5 * RENDERHEIGHT / 6); sf::Text timer2("", font, 80); timer2.setPosition(RENDERWIDTH / 2 - timer2.getLocalBounds().width, 5 * RENDERHEIGHT / 6); int time1 = 0; int time2 = 0; //gameover sf::Text gameover("GAME OVER", font, 120); gameover.setColor(sf::Color::Red); gameover.setPosition(0, RENDERHEIGHT / 3); //game loop while(player1Score + player2Score != 3) { sf::Event event; while(pong.pollEvent(event)) { if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) pong.close(); } //score for player one winning if(player1Score == player2Score + 1) { timer1.setString(convertInt(time1 += 2)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 2) { timer1.setString(convertInt(time1 += 4)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 3) { timer1.setString(convertInt(time1 += 8)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 4) { timer1.setString(convertInt(time1 += 16)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 5) { timer1.setString(convertInt(time1 += 32)); timer2.setString(convertInt(time2 += 1)); } if(player1Score == player2Score + 6) { timer1.setString(convertInt(time1 += 64)); timer2.setString(convertInt(time2 += 1)); } //score on equals if(player1Score == player2Score) { timer1.setString(convertInt(time1 += 1)); timer2.setString(convertInt(time2 += 1)); } //score for player two winning if(player2Score == player1Score + 1) { timer2.setString(convertInt(time2 += 2)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 2) { timer2.setString(convertInt(time2 += 4)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 3) { timer2.setString(convertInt(time2 += 8)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 4) { timer2.setString(convertInt(time2 += 16)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 5) { timer2.setString(convertInt(time2 += 32)); timer1.setString(convertInt(time1 += 1)); } if(player2Score == player1Score + 6) { timer2.setString(convertInt(time2 += 64)); timer1.setString(convertInt(time1 += 1)); } //player 1 movement if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) player1.move(0, -10); else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) player1.move(0, 10); //player 2 movement if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) player2.move(0, -10); else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) player2.move(0, 10); //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter) { ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x) { if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y) { ballVelX = (ballVelX - 1) * -1; bounce.play(); } else { point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 30) p2Len -= 10; player2.setSize(sf::Vector2f(10, p2Len)); if (p1Len < 80) p1Len += 10; player1.setSize(sf::Vector2f(10, p1Len)); } } //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x) { if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y) { ballVelX = (ballVelX + 1) * -1; bounce.play(); } else { point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 30) p1Len -= 10; player1.setSize(sf::Vector2f(10, p1Len)); if (p2Len < 80) p2Len += 10; player2.setSize(sf::Vector2f(10, p2Len)); } } //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(timer1); pong.draw(timer2); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.draw(ncp); pong.display(); while(player1Score + player2Score == 3) { if(player1Score > player2Score) timer1.setString(convertInt(time1 += 10000)); if(player1Score < player2Score) timer2.setString(convertInt(time2 += 10000)); pong.clear(sf::Color::Black); pong.draw(score1); pong.draw(timer1); pong.draw(timer2); pong.draw(score2); pong.draw(gameover); pong.display(); counter(3); break; } } std::stringstream ss; ss.str (timer1.getString()); std::string scorePlayer1 = ss.str(); ss.str (timer2.getString()); std::string scorePlayer2 = ss.str(); std::cout << "Final Score:" << '\n'; std::cout << "Player1: " + scorePlayer1 << '\n'; std::cout << "Player2: " + scorePlayer2 << '\n'; std::ofstream myfile ("highscores.txt", std::ofstream::in | std::ofstream::out | std::ofstream::app); if (myfile.is_open()) { myfile << scorePlayer1 << std::endl; myfile << scorePlayer2 << std::endl; myfile.close(); } else std::cout << "Unable to open file"; return 0; }
int main() { //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName", sf::Style::Fullscreen); pong.setMouseCursorVisible(false); pong.setFramerateLimit(60); //music sf::Music bgm; bgm.openFromFile("bgm.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam2.wav"); sf::Sound point; point.setBuffer(buffer2); //player 1 properties int p1Len = 80; sf::RectangleShape player1(sf::Vector2f(10, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 150; sf::RectangleShape player2(sf::Vector2f(10, p2Len)); player2.setFillColor(sf::Color(255, 50, 50)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(10, 25); ball.setFillColor(sf::Color(255,255,255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 2; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; //BONUS properties /* sf::CircleShape bonus(10, 25); ball.setFillColor(sf::Color(0,255,0)); ball.setPosition(RENDERWIDTH / 2 , RENDERHEIGHT / rand ()%10+1); float BONUSSPEED = 2; float bonusVelX = -BONUSSPEED, bonusVelY = -BONUSSPEED; float bonusX = RENDERWIDTH / 2 - bonus.getRadius(), bonusY = RENDERHEIGHT / 2 - bonus.getRadius(); float bonusDiameter = bonus.getRadius() * 2; */ //score text sf::Font font; font.loadFromFile("/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Bold.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); //game loop while(pong.isOpen()) { sf::Event event; while(pong.pollEvent(event)) { if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) pong.close(); } //player 1 movement if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){ player1.move(0, -10);} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){ player1.move(0, 10); } //MOVIMIENTO GOLPE PLAYER1 if (player1.getPosition().x <= 10){ if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){ player1.move(10, 0); }} //player 2 movement if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) player2.move(0, -10); else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) player2.move(0, 10); //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){ player2.move(-10, 0); }} //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter) { ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x) { if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y) { ballVelX = (ballVelX - 1) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); } else { point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8){ BALLSPEED += 0.2; } ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 30) p2Len -= 10; player2.setSize(sf::Vector2f(10, p2Len)); if (p1Len < 80) p1Len += 10; player1.setSize(sf::Vector2f(10, p1Len)); } } //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x) { if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y) { ballVelX = (ballVelX + 1) * -1; ball.setFillColor(sf::Color(255,0,0)); bounce.play(); } else { point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 30) p1Len -= 10; player1.setSize(sf::Vector2f(10, p1Len)); if (p2Len < 80) p2Len += 10; player2.setSize(sf::Vector2f(10, p2Len)); } } //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.display(); } return 0; }
int main(){ ////////////////////GAME PROPERTIES/////////////////////////////////// //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName"); pong.setMouseCursorVisible(false); pong.setFramerateLimit(60); //music sf::Music bgm; bgm.openFromFile("musica.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam2.wav"); sf::Sound point; point.setBuffer(buffer2); sf::SoundBuffer buffer3; buffer3.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); //perfecti hit!!!!! sf::Sound perfecthit; perfecthit.setBuffer(buffer3); //player 1 properties int p1Len = 80; sf::RectangleShape player1(sf::Vector2f(15, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 80; sf::RectangleShape player2(sf::Vector2f(15, p2Len)); player2.setFillColor(sf::Color(0, 255, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(7, 25); ball.setFillColor(sf::Color(255,255,255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 2; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; sf::Font font; font.loadFromFile("/usr/share/cups/fonts/FreeMonoOblique.ttf"); font.loadFromFile("/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Bold.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4, 0); ///////////////FINISH PROPERTIES////////////////////////////////////////////////////// CvCapture* capture =0; capture = cvCaptureFromCAM(1); if(!capture){ printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvZero(imgTracking); //covert the image, 'imgTracking' to black cvNamedWindow("Video"); cvNamedWindow("Ball"); //iterate through each frames of the video while(true){ frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh = GetThresholdedImage(imgHSV, 94, 169, 127, 143, 251, 229); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player100 = trackObject(imgThresh, 255, 0, 0, 1); IplImage* imgHSV2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV2, CV_BGR2HSV); IplImage* imgThresh2 = GetThresholdedImage(imgHSV2, 22, 64, 152, 50, 134, 256); cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player200 = trackObject(imgThresh2, 0, 255, 0, 2); // Add the tracking image and the frame cvAdd(frame, imgTracking, frame); cvShowImage("Ball", imgThresh); cvShowImage("Ball2", imgThresh2); cvShowImage("Video", frame); //Clean up used images cvReleaseImage(&imgHSV); cvReleaseImage(&imgHSV2); cvReleaseImage(&imgThresh); cvReleaseImage(&imgThresh2); cvReleaseImage(&frame); //Wait 10mS int c = cvWaitKey(10); //If 'ESC' is pressed, break the loop if((char)c==27 ) break; //////////////////////////////Game//////////////////////////////////// //player 1 movement if(player100==1){ player1.move(0, -10);} else if(player100==-1){ player1.move(0, 10); } //MOVIMIENTO GOLPE PLAYER1 if(player100==0){ player1.move(0,0);} if (player1.getPosition().x <= 10){ if(player100==2){ player1.move(10, 0); }} //player 2 movement if(player200==1) player2.move(0, -10); else if(player200==-1) player2.move(0, 10); if(player200==0){ player2.move(0,0);} //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(player200==-2){ player2.move(-10, 0); }} //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter) { ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x) { if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y){ if (player1.getPosition().x > 14){ ballVelX = (ballVelX - 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player1.getPosition().x <= 14){ ballVelX = (ballVelX - 1) * -1; ball.setFillColor(sf::Color(0,0,255)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8){ BALLSPEED += 0.2; } ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 40) p2Len -= 10; player2.setSize(sf::Vector2f(15, p2Len)); if (p1Len < 100) p1Len += 10; player1.setSize(sf::Vector2f(15, p1Len)); } } //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x) { if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y) { if (player2.getPosition().x < (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player2.getPosition().x >= (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 1) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 40) p1Len -= 10; player1.setSize(sf::Vector2f(15, p1Len)); if (p2Len < 100) p2Len += 10; player2.setSize(sf::Vector2f(15, p2Len)); } } //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.display(); /////////////////////Finish Game///////////////////////////////// } cvDestroyAllWindows() ; cvReleaseImage(&imgTracking); cvReleaseCapture(&capture); return 0; }
int main() { //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName"); //, sf::Style::Fullscreen pong.setMouseCursorVisible(false); pong.setFramerateLimit(24); /* //music sf::Music bgm; bgm.openFromFile("musica.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); */ //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam2.wav"); sf::Sound point; point.setBuffer(buffer2); sf::SoundBuffer buffer3; buffer3.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); //perfecti hit!!!!! sf::Sound perfecthit; perfecthit.setBuffer(buffer3); //player 1 properties int p1Len = 80; sf::RectangleShape player1(sf::Vector2f(15, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 80; sf::RectangleShape player2(sf::Vector2f(15, p2Len)); player2.setFillColor(sf::Color(0, 255, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(7, 25); ball.setFillColor(sf::Color(255,255,255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 2; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; sf::Font font; font.loadFromFile("/usr/share/cups/fonts/FreeMonoOblique.ttf"); font.loadFromFile("/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Bold.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4, 0); //game loop //while(pong.isOpen()) /////////////////////Declaraciones//////////////////////////////// IplImage* frame=0; IplImage* imgTracking; CvCapture* capture =0; IplImage* imgThresh; IplImage* imgThresh2; sf::Event event; int player100=0; int player200=0; capture = cvCaptureFromCAM(1); for(int i=0;i<1000;++i) { if(!capture){ printf("Capture failure\n"); return -1; } frame = cvRetrieveFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV imgThresh = GetThresholdedImage(imgHSV, 24, 0, 224, 46, 89, 256); cvReleaseImage(&imgHSV); delete(imgHSV); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player100 = trackObject(imgThresh, 255, 0, 0, 1); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IplImage* imgHSV2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV2, CV_BGR2HSV); imgThresh2 = GetThresholdedImage(imgHSV2, 24, 0, 224, 46, 89, 256); cvReleaseImage(&imgHSV2); delete(imgHSV2); cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player200 = trackObject(imgThresh2, 0, 255, 0, 2); /* //player 1 movement if( player100 == 1 ) { player1.move(0, -10);} else if(player100==-1){ player1.move(0, 10); } //MOVIMIENTO GOLPE PLAYER1 if (player1.getPosition().x <= 10){ if(player100==2){ player1.move(10, 0); }} //player 2 movement if(player200==1) player2.move(0, -10); else if(player200==-1) player2.move(0, 10); //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(player200==-2){ player2.move(-10, 0); }} //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter) { ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x) { if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y){ if (player1.getPosition().x > 14){ ballVelX = (ballVelX - 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player1.getPosition().x <= 14){ ballVelX = (ballVelX - 1) * -1; ball.setFillColor(sf::Color(0,0,255)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8){ BALLSPEED += 0.2; } ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 40) p2Len -= 10; player2.setSize(sf::Vector2f(15, p2Len)); if (p1Len < 100) p1Len += 10; player1.setSize(sf::Vector2f(15, p1Len)); } }//fin IF //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x) { if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y) { if (player2.getPosition().x < (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player2.getPosition().x >= (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 1) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); }//fin if elseh } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 40) p1Len -= 10; player1.setSize(sf::Vector2f(15, p1Len)); if (p2Len < 10) p2Len += 10; player2.setSize(sf::Vector2f(15, p2Len)); }//fin else }//fin IF */ //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.display(); }//fin for cvReleaseCapture(&capture); cvReleaseImage(&imgThresh); cvReleaseImage(&imgThresh2); cvReleaseImage(&frame); cvReleaseImage(&imgTracking); //cvReleaseImage(&imgTemp); revisar este puntero delete(moments); return 0; }